Please try this: public static extern Int32 PlaceInstance( UInt32 Handle, UInt16 ParticleID, UInt16 Layer, Byte[] InstanceName, float X, float Y, Byte[] Path, IntPtr DstPath ); Call it as follows: PlaceInstance(handle, 123, 222, Encoding.ASCII.GetBytes("name"), 10, 50, null, IntPtr.Zero); Note you can's pass strings. Strings are unicode and your DLL seems to expect ASCII. If you're expecting DstPath back, you would have to pass an instance of IntPtr. After call is done, cast it to Int32*, get value at this pointer, cast it to IntPtr and pass it it Marshal.PtrToStringBSTR() to get this string. Best regards, Ilya This posting is provided "AS IS" with no warranties, and confers no rights. [quoted text, click to view] "Xinjie ZHANG" <xjzhang78@hotmail.com> wrote in message news:edSk8EFDFHA.3416@TK2MSFTNGP09.phx.gbl...
I use a third-party C library in my .NET CF application. The method signature for one of the C functions is: int PlaceInstance( U32 Handle, U16 ParticleID, U16 Layer, char* InstanceName, SFLOAT X, SFLOAT Y, char* Path, char** DstPath ); In C++ applications, it can be called like this: PlaceInstance(handle, 123, 222, "name", 10, 50, NULL, NULL) I have tried many ways to make it work, but unfortunately, I can not :( public static extern int PlaceInstance( uint Handle, ushort ParticleID, ushort Layer, string InstanceName, float X, float Y, string Path, out StringBuilder DstPath ); call ==> StringBuilder builder = new StringBuilder(1024); PlaceInstance(handle, 123, 222, "name", 10, 50, null, out builder) public static unsafe extern int PlaceInstance( uint Handle, ushort ParticleID, ushort Layer, string InstanceName, float X, float Y, char* Path, char** DstPath ); call ==> PlaceInstance(handle, 123, 222, "name", 10, 50, (char*)0, (char**)0) public static unsafe extern int PlaceInstance( uint Handle, ushort ParticleID, ushort Layer, string InstanceName, float X, float Y, byte* Path, byte** DstPath ); call ==> PlaceInstance(handle, 123, 222, "name", 10, 50, (byte*)0, (byte**)0) public static extern int PlaceInstance( uint Handle, ushort ParticleID, ushort Layer, string InstanceName, float X, float Y, IntPtr Path, out IntPtr DstPath ); call ==> IntPtr ptr = IntPtr.Zero; PlaceInstance(handle, 123, 222, "name", 10, 50, IntPtr.Zero, out ptr ) They always fail in PlaceInstance call with "Object reference not set to an instance of an object" error. It makes me crazy :( Could anyone helps me out? Thanks !
There is no way a P/Invoke can produce NullReferenceException. It has to = occur on the previous line of code. Recheck your code. As for the P/Invoke definition, the last one is correct, but if you = don't care about the returned value for DstPath, you should simply = define it as int and pass 0 as Ilya points out. Regarding the string recovery from the pointer returned to you, I'd use = Marshal.PtrToStringUni --=20 Alex Feinman --- Visit http://www.opennetcf.org [quoted text, click to view] "Xinjie ZHANG" <xjzhang78@hotmail.com> wrote in message = news:edSk8EFDFHA.3416@TK2MSFTNGP09.phx.gbl...
I use a third-party C library in my .NET CF application. The method = signature for one of the C functions is: int PlaceInstance( U32 Handle, U16 ParticleID, U16 Layer, char* = InstanceName, SFLOAT X, SFLOAT Y, char* Path, char** DstPath ); In C++ applications, it can be called like this: PlaceInstance(handle, 123, 222, "name", 10, 50, NULL, NULL) I have tried many ways to make it work, but unfortunately, I can not = :( public static extern int PlaceInstance( uint Handle, ushort = ParticleID, ushort Layer, string InstanceName, float X, float Y, string = Path, out StringBuilder DstPath ); call =3D=3D> StringBuilder builder =3D new StringBuilder(1024); PlaceInstance(handle, 123, 222, "name", 10, 50, null, out builder) public static unsafe extern int PlaceInstance( uint Handle, ushort = ParticleID, ushort Layer, string InstanceName, float X, float Y, char* = Path, char** DstPath ); call =3D=3D> PlaceInstance(handle, 123, 222, "name", 10, 50, (char*)0, = (char**)0) public static unsafe extern int PlaceInstance( uint Handle, ushort = ParticleID, ushort Layer, string InstanceName, float X, float Y, byte* = Path, byte** DstPath ); call =3D=3D> PlaceInstance(handle, 123, 222, "name", 10, 50, (byte*)0, = (byte**)0) public static extern int PlaceInstance( uint Handle, ushort = ParticleID, ushort Layer, string InstanceName, float X, float Y, IntPtr = Path, out IntPtr DstPath ); call =3D=3D>=20 IntPtr ptr =3D IntPtr.Zero; PlaceInstance(handle, 123, 222, "name", 10, 50, IntPtr.Zero, out ptr = ) They always fail in PlaceInstance call with "Object reference not set = to an instance of an object" error. It makes me crazy :( Could anyone =
I use a third-party C library in my .NET CF application. The method = signature for one of the C functions is: int PlaceInstance( U32 Handle, U16 ParticleID, U16 Layer, char* = InstanceName, SFLOAT X, SFLOAT Y, char* Path, char** DstPath ); In C++ applications, it can be called like this: PlaceInstance(handle, 123, 222, "name", 10, 50, NULL, NULL) I have tried many ways to make it work, but unfortunately, I can not :( public static extern int PlaceInstance( uint Handle, ushort ParticleID, = ushort Layer, string InstanceName, float X, float Y, string Path, out = StringBuilder DstPath ); call =3D=3D> StringBuilder builder =3D new StringBuilder(1024); PlaceInstance(handle, 123, 222, "name", 10, 50, null, out builder) public static unsafe extern int PlaceInstance( uint Handle, ushort = ParticleID, ushort Layer, string InstanceName, float X, float Y, char* = Path, char** DstPath ); call =3D=3D> PlaceInstance(handle, 123, 222, "name", 10, 50, (char*)0, = (char**)0) public static unsafe extern int PlaceInstance( uint Handle, ushort = ParticleID, ushort Layer, string InstanceName, float X, float Y, byte* = Path, byte** DstPath ); call =3D=3D> PlaceInstance(handle, 123, 222, "name", 10, 50, (byte*)0, = (byte**)0) public static extern int PlaceInstance( uint Handle, ushort ParticleID, = ushort Layer, string InstanceName, float X, float Y, IntPtr Path, out = IntPtr DstPath ); call =3D=3D>=20 IntPtr ptr =3D IntPtr.Zero; PlaceInstance(handle, 123, 222, "name", 10, 50, IntPtr.Zero, out ptr ) They always fail in PlaceInstance call with "Object reference not set to = an instance of an object" error. It makes me crazy :( Could anyone =
Don't see what you're looking for? Try a search.
|