使用dllimport在C#中加载C dll [英] load a C dll in C# with dllimport

查看:141
本文介绍了使用dllimport在C#中加载C dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

我有来自我项目中另一个团队的AC dll。

我尝试在C中加载某个功能#

函数看起来像那样:

Void GetData(TCHAR名称[],字符**地址,DWORD *大小)

如何在C#中声明等效函数?

我正在使用DllImport ...

谢谢

Dj4400

Hi.
I Have A C dll That I Got From Another team in My project.
I Try to Load A Certain Function From it in C#
The function looks Like That:
Void GetData(TCHAR name [], char **address, DWORD *size)
How Should I declare The equivilant Function in C# ?
I am Using DllImport...
Thanks
Dj4400

推荐答案

使用不安全和char *。

函数看起来像这样:

public static extern void GetData(stringBuilder str,char ** addr,uint * size)
Using unsafe and char*.
The Function looks Like This:
public static extern void GetData(stringBuilder str, char **addr, uint *size)


我用这种方式调用函数,指针指向参数。这对你的功能也有好处。



字符串和字符串列表将与int指针一起使用:

I use to use this way call functions with pointer to pointer arguments. This may good for your function also.

The string and the string list would be used with int pointers:
public static extern void GetData( System.IntPtr name, System.IntPtr address, ref Int32 Size)





在调用alloc之前名称:





before call alloc name:

IntPtr name = Marshal.AllocHGlobal(Marshal.SystemDefaultCharSize * <maxsize>);





然后致电:



then call:

GetData(ref name, ref address, ref Size);





你可以得到Nam e和这样的地址:





you can get the Name and Address like this way:

string Name = GlobalMarshaller.StringFromIntPtr(name);
string[] Adress = GlobalMarshaller.StringFromIntPtr(address, <maxcount>, <maxsize>);



然后清理!


Then clean up!

Marshal.FreeHGlobal(name);
Marshal.FreeHGlobal(adress);


您好dj4400u,



使用< br $>


public static extern void GetData(stringbuilder sb,string,double);



同时确保你的dll path is right。首先使用硬编码路径然后尝试使用运行时路径。



如有任何问题,请告诉我。



谢谢

sjs
Hi dj4400u,

Use

public static extern void GetData(stringbuilder sb,string,double);

Also make sure that your dll path is right.make it hardcoded path first and then try with runtime path.

Please let me know in case of any issue.

Thanks
sjs


这篇关于使用dllimport在C#中加载C dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆