如何传递结构的地址到Void指针[void *]在C#到VC ++ Dll [英] How to Pass address of Stucture to Void pointer[void*] in C# to VC++ Dll
本文介绍了如何传递结构的地址到Void指针[void *]在C#到VC ++ Dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//结构:
struct MYDATA{
char calls[4069];
char Desc[4096];
char error[1024];
} ;
//测试函数
char *Argv[] = { "ToolName", "USername", "192.168.2", "3", "400"};
typedef void* ( *__stdcall pCstartSIPSessionint)(char **argv );
typedef struct MYDATA*(*__cdecl pgetStat) ();
pCstartSIPSessionint startSessionint = (pCstartSIPSessionint )GetProcAddress(HMODULE (hGetProcIDDLL),"startSessionint");
// Dll导入
HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("E:\\MyDll.dll"));
pgetStat getStat ;
getStat = (pgetStat ) startSessionint(Argv);
MYDATA *mydata;
if(getStat != NULL)
mydata= getStat();
printf("\n mydata\n %s ", mydata->call);
printf("\n mydata\n %s ", mydata->summary);
printf("\n mydata\n %s ", mydata->error);
如何转换C#中的代码?
我在C#
中将结构地址传递给Void *我只能传递值,而是将Address传递给结构。
How to Convert the code in C#? And i have confused to pass address of stucture to Void* in C# I can able to pass only values, how can pass Address to the structure.
提前感谢...!
推荐答案
结构应为:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MYDATA
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4096)]
public string calls;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4096)]
public string Desc;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string error;
}
我假设问题中的4069值是拼写错误。
I assume the 4069 value in the question is a typo.
第一个函数是:
[DllImport(dllname, CharSet = CharSet.Ansi)]
public static extern IntPtr startSessionint(string[] argv);
然后将函数指针转换为委托:
Then convert the function pointer to a delegate:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate IntPtr getStatDelegate();
....
IntPtr ptr = startSessionint(argv);
if (ptr == IntPtr.Zero)
// handle error
getStatDelegate getStat = (getStatDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(getStatDelegate));
现在可以调用函数:
IntPtr structPtr = getStat();
然后编组到结构:
MYDATA data = (MYDATA)Marshal.PtrToStructure(structPtr, typeof(MYDATA));
这篇关于如何传递结构的地址到Void指针[void *]在C#到VC ++ Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文