unsafe code in c#

G

gobusk

Hi all,
i have created a unmanaged VC++ dll and then created a wrapper class
for it . i have included this wrapper class in my c# by using add
reference .The function takes ushort* as the input .i.e. i am should
pass a string as an input to the function

Function Prototype is this:
void WCConvertImg(unsigned short __nogc * ,unsigned short __nogc *);
now that I am facing problem in passing parameter to this function. I
tried first by using unsafe in C# and then with marshal class., I
cannot assign the address of string variable to ushort * variable in
C#, Its not allowed in managed code .
Regards
Gobu sk
 
G

Guest

you need to first pin the string on the heap and assign it to a char*, then you can cast the char* to a ushort*. here's an example

using System;
using System.Collections;

public class MyClass
{
public unsafe static void Main()
{
string s = "some string";
fixed( char* p = s )
{
for(ushort* p2 = (ushort*)p; *p2 != 0x0; p2++ )
Console.Write( (char)*p2 );
Console.ReadLine();
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top