简体   繁体   中英

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


I need to access functionality from win32 dll , for that I am using [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.

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM