C#中如何获得非托管DLL中的输出参数?
本人初学C#,请各位前辈帮忙!
我在C#中调用VC编写的DLL,要获得输出参数的值,输出参数是一个结构体。当结构体中只包含数值型变量时没有问题,但当结构体中包含字符数组变量时就什么都传不出来了。
VC的DLL代码如下:
typedef struct ts
{
int n;
float f;
char str1[10];
}tt;
int Test(tt *t)
{
t->n = 100;
t->f = 1.18;
strcpy(t->str1, "OK1");
return 0;
}
C#的调用代码为:
1。引入参数结构和DLL的接口函数
[StructLayout(LayoutKind.Sequential)]
public class tt
{
public int n;
public float f;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=10)]
//也试过 [MarshalAs(UnmanagedType.LPStr)] ,都不行
public string str1;
}
[DllImport("testdll.dll",CharSet=CharSet.Ansi)]
public static extern int Test(tt t);
2。函数调用
tt t = new tt();
int i;
i = SMGPTransport.Test(t);
不知道问题出在那里,就高手帮忙分析,先谢了!
问题点数:0、回复次数:4Top
1 楼cnhgj(戏子) (没时间练太极)回复于 2004-12-03 19:33:01 得分 0
public static extern int Test(ref tt t);
i = SMGPTransport.Test(ref t);Top
2 楼eaglemp()回复于 2004-12-03 19:45:16 得分 0
非常感谢你的回复!
但这样我也试过了,会抛出异常
未处理的“System.ExecutionEngineException”类型的异常出现在 WindowsApplication1.exe 中。Top
3 楼x0000()回复于 2004-12-03 21:02:59 得分 0
我怎么连该dll都不能注册呢?
// vdll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
typedef struct ts
{
int n;
float f;
char str1[10];
}tt;
int Test(tt *t)
{
t->n = 100;
t->f = (float)1.18;
strcpy(t->str1, "OK1");
return 0;
}Top
4 楼nmlvjun(网事如风)回复于 2004-12-17 17:05:25 得分 0
ding
结构体中包含字符数组变量时怎么传值?Top




