开发者

How to Access/marshall char *variable from dll import in C#

I need to access functionality from win32 dll , for that I am 开发者_如何学Pythonusing [dllimport] in C# code.

what exact method signature i need to create with [dllimport] for the following c++ methods

void GetGeneratedKey(char *code, int len, char *key)

pls help in this.

Thanks

nRk


This depends highly on what is happening to the variables key and code in the native C function. Based on the signature I am guessing that code is being read from and key is being written to. If that is the case then try the following signature

[DllImport("SomeDll")]
public static extern void GetGeneratedKey(
  [In] string code,
  [In] int length,
  StringBuilder key);


Just use string. Should just work.


everybody for the quick reply and support
I used method signature like the below

VC++ method signature
void GetGeneratedKey(char *code, int len, char *key)

C# signature
[DllImport("SomeDll")]
public static extern void GetGeneratedKey(byte[] code, int len, bute key);


nRk


Here is my solution for Unmanaged case.

C++

 void GetGeneratedKey(
 const char *code,
 int length,
 char *key);

C#

[DllImport("Some.Dll")]
public static extern void GetGeneratedKey(
[MarshalAs(UnmanagedType.LPStr)]string code,
int length,
[MarshalAs(UnmanagedType.LPStr)]StringBuilder key);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜