简体   繁体   中英

Convert IntPtr to int* in C#?

I have a C++ DLL returning an int* to a C# program. The problem is the int* in C# remains null after the assignment.

When I assign the C++ result to an IntPtr , I get a correct non-null value. However any attempt to convert this to an int* results in 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* ?

Thanks! Alan

You cppFunction declaration should be something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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