简体   繁体   English

在C#中将IntPtr转换为int *?

[英]Convert IntPtr to int* in C#?

I have a C++ DLL returning an int* to a C# program. 我有一个将C int*程序返回int*的C ++ DLL。 The problem is the int* in C# remains null after the assignment. 问题是赋值后C#中的int*仍然为null

When I assign the C++ result to an IntPtr , I get a correct non-null value. 当我将C ++结果分配给IntPtr ,我得到了正确的非null值。 However any attempt to convert this to an int* results in null . 但是,将其转换为int*任何尝试都将导致null

I've tried: 我试过了:

IntPtr intp = cppFunction ();           // Non-null.

int* pi = (int *) intp;                 // Results in null.

int* pi = (int *) intp.ToPointer ();    // Results in null.


void* vp = intp.ToPointer ();

int* pi = (int *) vp;                   // Results in null, but vp is non-null.

How can I get a non-null int* ? 如何获得非null int*

Thanks! 谢谢! Alan 艾伦

You cppFunction declaration should be something like: 您的cppFunction声明应类似于:

void cppFunction(ref int* ptr) {
   ptr = somevalue;
}

That should solve your problem. 那应该解决您的问题。

You may find this useful also: http://www.pcreview.co.uk/forums/thread-2902012.php 您可能还会发现这很有用: http : //www.pcreview.co.uk/forums/thread-2902012.php

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

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