Here is a code fragment from some C++ code:
Header File 1 declaration (this is supplied by a third party vendor:
class User;
DLL_API User* pickUser( Global* g, User** users, int numUsers );
The documentation states that "The 'users' are a list of User pointers"
I have a C++ class that wraps this api as follows:
DLL_API long MyPickUser(long g, long** users , int numUsers)
{
return( (long) ::pickUser( (Global* ) g, (User** ) users, numUsers));
}
In my CSharp class I have set up the following:
[DllImport("MyWrapper.dll", CharSet=CharSet.Auto)]
private static extern int MyPickUser(int g, ref int[] users, int numUsers);
internal int PickUser(ref int[] users,int numUsers)
{
int ret;
try
{
ret = MyPickUser(miGlobal, ref users,numUsers);
return ret;
}
catch (Exception ex)
{
HandleError(ex,"PickUser");
return 0;
}
}
public int PickUser()
{
int[] iUsers;
iUsers = new int[1];
//The UserId is a pointer to a User obtained from a previous call to the
C++ module.
iUsers[0] = Users.Item(0).UserId;
int usr = PickUser(ref iUsers,1);
return usr;
}
From what I can tell the third party dll is not getting the array of users
and is returning a 0 (there indicator an error occurred). How do you do this
correctly?
"Peter Huang" [MSFT] - 06 Jun 2005 06:27 GMT
Hi
Based on my research, so far .NET framework does not support marshal a
dynamically C Style array to C++ byref.
So far we can do it as follows to do the job.
[C#]
[DllImport(@"..\..\..\CPPDLL\Debug\CPPDLL.dll")]
private static extern int MyPickUser(int g, ref IntPtr users, int
numUsers);
[STAThread]
static void Main(string[] args)
{
int[] us = new int[2]{10,20};
int sz =us.Length*Marshal.SizeOf(typeof(int));
IntPtr unManagedIntArray=Marshal.AllocCoTaskMem(sz);
Marshal.Copy(us,0,unManagedIntArray,2);
MyPickUser(2,ref unManagedIntArray,2);
Marshal.Copy(unManagedIntArray,us,0,2);
Console.WriteLine("After Changed");
foreach( int i in us)
Console.WriteLine(i);
Marshal.FreeCoTaskMem(unManagedIntArray);
}
[C++]
CPPDLL_API long MyPickUser(long g, long** users , int numUsers)
{
printf("Before change:\n");
for(int i=0;i<numUsers;i++)
{
printf("%d \n",(*users)[i]);
(*users)[i] *=2;//change the value
}
return 42;
}
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Bruce Parker - 07 Jun 2005 04:25 GMT
Thanks for the answer. The one thing I didn't mention was I am doing this
with the .NET Compact framework and it appears marshalling as you suggested
is not supported. Is there an alternative way for the Compact framework?
> Hi
>
[quoted text clipped - 39 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 07 Jun 2005 08:02 GMT
Hi
So far I am researching the issue, and I will update you with new
information ASAP.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 08 Jun 2005 09:01 GMT
Hi
I think you may try to P/Invoke into LocalAlloc/LocalFree to do the memory
alloc and free on the unmanaged heap to replace the
Marshal.AllocCoTaskMem/FreeCoTaskMem.
Here is a link or your reference.
Creating a P/Invoke Library
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/PInvokeLib.asp
Also since we do many unmanaged API call, you may consider to use Managed
C++ to wrap the call.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Bruce Parker - 08 Jun 2005 14:46 GMT
I will try this. The link is also useful for some other things I am looking
to do.
Thanks
> Hi
>
[quoted text clipped - 17 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 09 Jun 2005 03:10 GMT
Hi
Thanks for your quickly reply!
However, if you still have any concern, please feel free to post here.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.