c#调用c编写的dll 关于指针问题 指针c#cstruct 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用c#调用一个c编写的dll 返回一个指针 我在c#中该怎么通过这个指针去找对应的值 yourtype t = (yourtype)Marshal.PtrToStructure(ptr,typeof(yourtype)) 谢谢 还有个问题C语言函数为int vip_fluoro_set_prms(int structType, void* structPtr)对应的c#函数 void*函数该怎么改? 你是说在C#里怎么引用吗?int vip_fluoro_set_prms(int structType, int structPtr)其中structPtr为获取Struct的IntPtr后传IntPtr.ToInt32()进去 你是说在C#里怎么引用吗?int vip_fluoro_set_prms(int structType, int structPtr)其中structPtr为获取Struct的IntPtr后传IntPtr.ToInt32()进去谢谢谢 《精通.NET互操作 P_Invoke,C++Interop和COM Interop》我最近找了这本书,有些心得,你有时间看看 [DllImport(path, EntryPoint = "OCR")]static extern IntPtr OCR...string result = Marshal.PtrToStringAnsi(OCR(xxx...)) 这个示例代码有问题。int j生存在堆栈上,当方法返回,堆栈回滚后,指向j的指针就成了野指针。 这个示例代码有问题。int j生存在堆栈上,当方法返回,堆栈回滚后,指向j的指针就成了野指针。我将这段代码写成函数时,能确保ptr_dll_test->ptr_int这个值时正确的。这又是为什么?关于堆栈生存周期有什么资料可以推荐我看吗? 野指针有不确定性,比如下面的例子:// MyDll.cppstruct MyStruct{ char Name[32]; int* PtrM; int* PtrN;};int i = 88;extern "C" _declspec(dllexport) void Test(MyStruct* pMy){ int j = 99; pMy->PtrM = &i; pMy->PtrN = &j; //<--有问题,指向堆栈中的数据 strncpy(pMy->Name, "hello world", 32);}堆栈中的数据可能被方法调用覆盖:static void Main(string[] args){ MyStruct my = new MyStruct(); Test(ref my); int m1 = Marshal.ReadInt32(my.PtrM); // m1 = 88 int n1 = Marshal.ReadInt32(my.PtrN); // n1 = -1 不是99 DoNothing(); int m2 = Marshal.ReadInt32(my.PtrM); // m2 = 88 int n2 = Marshal.ReadInt32(my.PtrN); // n2 = 0 被DoNothing覆盖了。}static int DoNothing(){ int i = 9; int j = i * 8; return i * j;}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]struct MyStruct{ [MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32)] public string Name; public IntPtr PtrM; public IntPtr PtrN;}[DllImport("MyDll", CallingConvention = CallingConvention.Cdecl)]static extern void Test(ref MyStruct myStruct); 解决了 定义时:WORD* buf= (WORD*)CoTaskMemAlloc(sizeof(WORD) * (512*512*2));最后在用完释放掉。 100分请教 C#自定义打印第几页共几页如何实现 关于解决linq并发冲突 的 ResolveAll 方法无效的问题 请推荐.net3.5围绕实例讲解操作的书籍 C# 和服务器有关 请教高手 在线等 来着有分! 关于IPAddress类实例化时参数的问题。 静态方法如何访问控件? 有谁使用过mike2.0 组件么?这个错误提示是什么问题呢? Base64、UrlEncode是否需要UrlDecode 各位有一些比较简单的c#例子吗?? 有没有可能这样。请各位过来讨论一下 是程序中的add缺少using指令或程序集 [求助]如何让一个exe程序(不是自己写的)可以最小化到托盘运行
C语言函数为int vip_fluoro_set_prms(int structType, void* structPtr)
对应的c#函数 void*函数该怎么改?
int vip_fluoro_set_prms(int structType, int structPtr)
其中structPtr为获取Struct的IntPtr后传IntPtr.ToInt32()进去
int vip_fluoro_set_prms(int structType, int structPtr)
其中structPtr为获取Struct的IntPtr后传IntPtr.ToInt32()进去谢谢谢
static extern IntPtr OCR...string result = Marshal.PtrToStringAnsi(OCR(xxx...))
int j生存在堆栈上,当方法返回,堆栈回滚后,指向j的指针就成了野指针。
int j生存在堆栈上,当方法返回,堆栈回滚后,指向j的指针就成了野指针。我将这段代码写成函数时,能确保ptr_dll_test->ptr_int这个值时正确的。这又是为什么?关于堆栈生存周期有什么资料可以推荐我看吗?
struct MyStruct
{
char Name[32];
int* PtrM;
int* PtrN;
};int i = 88;
extern "C" _declspec(dllexport) void Test(MyStruct* pMy)
{
int j = 99;
pMy->PtrM = &i;
pMy->PtrN = &j; //<--有问题,指向堆栈中的数据
strncpy(pMy->Name, "hello world", 32);
}堆栈中的数据可能被方法调用覆盖:static void Main(string[] args)
{
MyStruct my = new MyStruct();
Test(ref my); int m1 = Marshal.ReadInt32(my.PtrM); // m1 = 88
int n1 = Marshal.ReadInt32(my.PtrN); // n1 = -1 不是99 DoNothing(); int m2 = Marshal.ReadInt32(my.PtrM); // m2 = 88
int n2 = Marshal.ReadInt32(my.PtrN); // n2 = 0 被DoNothing覆盖了。
}static int DoNothing()
{
int i = 9;
int j = i * 8;
return i * j;
}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct MyStruct
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst = 32)]
public string Name;
public IntPtr PtrM;
public IntPtr PtrN;
}[DllImport("MyDll", CallingConvention = CallingConvention.Cdecl)]
static extern void Test(ref MyStruct myStruct);
最后在用完释放掉。