Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a native DLL written in C, which contains this function:

SomeFunction(int cmd, int param, unsigned char* data);


In my C# code on VS2005, how do I import this function via DllImport attribute? Note this function takes some parameters of native types.

Should I import this way:

C#
[DllImport("SomeNativeDll")]
public static extern int SomeFunction(int cmd, int param, ref byte data);
Posted
Updated 10-Nov-10 8:33am
v3

1 solution

It really depends on what the parameter is used for. It seems to me that data is a byte buffer, so your P/Invoke signature should be:

C#
public static extern int SomeFunction(int cmd, int param, out byte[] data);


You may use ref instead of out, if the method expects you to pass an initialized array.
 
Share this answer
 
v3
Comments
Jun Du 12-Nov-10 10:09am    
Thanks for the answer. Since my chunk of data is actually an input parameter to the call, I guess either "ref" keyword or none is good.
Nish Nishant 12-Nov-10 10:11am    
Yes, that's right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900