简体   繁体   English

如何在C#中从dll导入访问/编组char *变量

[英]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. 我需要从win32 dll访问功能,因为我在C#代码中使用[dllimport]。


what exact method signature i need to create with [dllimport] for the following c++ methods 我需要使用[dllimport]为以下c ++方法创建什么确切的方法签名

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

pls help in this. 请帮忙。

Thanks 谢谢
nRk NRK

This depends highly on what is happening to the variables key and code in the native C function. 这在很大程度上取决于本机C函数中变量keycode的变化。 Based on the signature I am guessing that code is being read from and key is being written to. 根据签名,我猜测正在读取code并正在写入key 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 . 只需使用string Should just work. 应该工作。

Here is my solution for Unmanaged case. 这是我对Unmanaged案例的解决方案。

C++ C ++

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

C# 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 VC ++方法签名
void GetGeneratedKey(char *code, int len, char *key) void GetGeneratedKey(char * code,int len,char * key)

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


nRk NRK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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