marshalling

To call a method that requires IntPtr, is it better to use /unsafe, or Marshal.AllocHGlobal?

I have a class that will have a few instances persistent throughout the duration of the application. These objects will each need to call a dll method that appends data from an existing float[] buffer, and passes the full dataset to a DLL method that accepts an IntPtr (float array), several times per second. Is it better to do it as un...

F# syntax for P/Invoke signature using MarshalAs

I'm unsure of the syntax for this. I'm trying to translate this C# code into F#. struct LASTINPUTINFO { public uint cbSize; public uint dwTime; } public class IdleTimer { [DllImport("User32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); } This i...

Boolean Marshalling with LayoutKind.Explicit, Is this broken or failing as designed?

First of all the Boolean type is said to have a default marshal type of a four-byte value. So the following code works: struct A { public bool bValue1; public int iValue2; } struct B { public int iValue1; public bool bValue2; } public static void Main() { in...

How can I ignore a field when marshalling a structure with P/Invoke

Hi, I want to marshal a structure for use with P/Invoke, but this struct contains a field that is only relevant to my managed code, so I don't want it to be marshaled since it doesn't belong in the native structure. Is it even possible ? I was looking for an attribute similar to NonSerialized for serialization, but it doesn't seem to ex...

C# - Buffer Corruption with Marshal.Copy()

I am receiving an IntPtr and an int specifying the number of bytes it points to. The data can contain any characters including null, EOL, etc. When trying the following, the buffer is corrupted: //buffer is the IntPtr //count is the number of bytes in 'buffer' byte[] test = new byte[count]; Marshal.Copy(buffer, test, 0, count); In...

C#: calling C++ DLL with char** argument

I would like to call this C++ function from my C# code: void GetArrayOfNames(char** names, int nbOfNames); To call it in C++, I just define an array of char*: char* aNames[20]; And allocate each name in a loop: for(int i-0; i<20; i++) { aNames[i] = new char[50]; } Then call: GetArrayOfNames(aNames, 20); In my C# code, I ...

C#: passing array of strings to a C++ DLL

I'm trying to pass some strings in an array to my C++ DLL. The C++ DLL's function is: extern "C" _declspec(dllexport) void printnames(char** ppNames, int iNbOfNames)<br> {<br> for(int iName=0; iName < iNbOfNames; iName++) { OutputDebugStringA(ppNames[iName]); } } And in C#, I load the function like this: [DllImport...

How do I implement marshalling in a VC++ ATL COM server?

While implementing my own IFilter I found that most likely some consumers will require marshalling from it. That is they QueryInterface() the IMarshal interface from my object. Proxy/stubs from MIDL can't be used - MIDL will not compile the filter.idl file - the latter uses custom structures and MIDL doesn't know what to do with them. S...

Copy bytes in memory to an Array in VB.NET

Hello, unfortunately I cannot resort to C# in my current project, so I'll have to solve this without the unsafe keyword. I've got a bitmap, and I need to access the pixels and channel values directly. I'd like to go beyond Marshal.ReadByte() and Marshal.WriteByte() (and definitely beyond GetPixel and SetPixel). Is there a way to put a...

Marshalling an array of structures from C++ to C#

In my C# code I'm trying to fetch an array of structures from a legacy C++ DLL (the code I cannot change). In that C++ code, the structure is defined like this: struct MyStruct { char* id; char* description; }; The method that I'm calling (get_my_structures) returns a pointer to an array of MyStruct structures: MyStruct* get...

How do I unmarshal a ruby object in java?

I have a an object that I'd like to grab the contents of in java. The only problem is that is is currently in ruby. irb(main):050:0> blah => "BAh7ByIeYXV0aGVudGljYXRpb25fc3RyYXRlZ2llczAiCXVzZXJpBg%253D%253D-\ -0cdecf4edfaa5cbe4693c9fb83b204c1256a54a6" irb(main):049:0> Marshal.load(Base64.decode64(blah)) => {"authentication_strategies"=...

Are double* and double** blittable types? C#

Hello, I have a question regarding marshalling of C++ arrays to C#. Does the double* automatically convert to double[]? I know double is a blittable type, so double from C++ is the same as double from C#. And what about double**, does it convert to double[,] ? I have the following unmanaged function: int get_values(double** param,in...

How can I prevent CompileAssemblyFromSource from leaking memory?

I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in memory. After the assembly has been garbage collected, my application uses more memory than it did before creating the assembly. My code is in a ASP.NET web app, but I've duplicated this problem in a WinForm. I'm using System.GC.Ge...

How do I register a proxy/stub for a COM interface defined by a third party?

There's Another Company that ships the product that consumes IAnotherCompanyInterface. We want to ship a COM object that implements IAnotherCompanyInterface. That interface is not Automation-compatible, so the next easiest option to enable marshalling is using a proxy/stub. Another Company doesn't ship the proxy/stub and doesn't want to....

Char * marshalling in C#

I have this function in Visual C++ DLL char * __stdcall GetMessage(int id) { char buffer[250]; . . . strcpy(buffer, entry); // entry is a char * that has allocated 250 chars return (char *)buffer; } i am trying to import this function from C# with the following code [DllImport("mydll.dll", CharSet=CharSet.Ansi)] public static extern ...

P/Invoke problem marshalling parameter

It seems I have yet another problem in the understanding of marshalling to C++ DLL. Here is the def of the C++ function & struct : #define SIZE_PLATE (28l) #define SIZE_HJT (15l) #define SIZE_DATE (10) typedef struct _tyrfdePlate { TCharA PlateID[SIZE_PLATE]; TInt32 NetworkID; TInt32 CityID; ...

Having a hard time pinvoking with a byte array.

I have an unmanaged method in a DLL with the following signature: extern "C" ErrorCodeEnum BuildSerialTriggerSetup(TriggerSetup_type sSetup, BYTE * pFPGACfgStream, UINT32 * piStreamLen) I have a pinvoke set up in C# with the following signature: [System.Runtime.InteropServices.DllImportAttribute("SerTrigLib.dll", EntryPoint = "BuildS...

.NET Remoting Singleton memory leak, TCP, Marshal by Reference

I am using the simplest example of remoting that I could find, sharing an object between a windows service and a windows forms program (client), running on the same machine. The service instantiates the object like this: serviceConfigRemote = new serviceConfigDataRemote(); serverChannel = new TcpServerChannel(9090); ChannelServices.Reg...

Unsafe code and fixed statements with StringBuilder

I was wondering about how passing a String or a StringBuilder to a C function which output a string by parameter. I've found a great answer in http://stackoverflow.com/questions/1687558/calling-unmanaged-function-from-c-should-i-pass-stringbuilder-or-use-unsafe-cod But I have a doubt. Anyone can explain to me why the garbage collector ...

Marshal struct problem with C#

I'm usign C# with P/Invoke to acces to a dll method. The definition of the method is the following: [DllImport("userManager.dll")] static extern int GetUsers(out IntPtr userList); And the struct layout I've done is the following: [StructLayout(LayoutKind.Sequential)] public class USER_LIST { public uint NumUsers; ...