marshalling

SAP .NET Connector: System exception thrown while marshaling .NET type...

My app sends lots and lots of data to SAP. To di this, it builds up an SAP table object and sends it over. I get this error somewhat regularly, but not reliably: System exception thrown while marshaling .NET type 20081219 to RFCTYPE_BCD at SAP.Connector.Rfc.RfcMarshal.NetFieldToRfcField(Object src, RFCTYPE type, Encoding encoding, By...

How do I convert a string array to a LPCWSTR in .NET to pass to a Win32 API function?

Hi all I have a piece of C# code that needs to convert a string array to a LPCWSTR to pass to a Win32 API function. I can't find nothing in the Marshal class that makes it straightforward. Does anybody knows how to do that? ...

When should I explicitly specify a StructLayout?

I'm fiddling with calling DLLs from C#, and came across the need to define my own structs. Lots of articles force a sequential layout for the struct with [StructLayout(LayoutKind.Sequential)] struct Foo ... So, I followed suite, and my programme worked. Now, when I took the line out, it still works. Why do I need it? ...

.NET marshalling speed

Hi all, I have a C++ method signature that looks like this: static extern void ImageProcessing( [MarshalAs(UnmanagedType.LPArray)]ushort[] inImage, [MarshalAs(UnmanagedType.LPArray)]ushort[] outImage, int inYSize, int inXSize); I've wrapped the function in timing methods, both internal and external. Inter...

Is there a way to call an unmanaged (not COM) dll from C# application?

Hello, Is there a way to use (reference) a DLL written in an unmanaged C++ (not a COM library) in my C# application? When I try to reference it from within Visual Studio, I get 'not a COM object' error message. Maybe there is some kind of translator\router that would COMify my DLL reference? I have no clue how COM and COM interop work...

Do I need to delete structures marshaled via Marshal.PtrToStructure in unmanaged code?

I have this C++ code: extern "C" __declspec(dllexport) VOID AllocateFoo(MY_DATA_STRUCTURE** foo) { *foo = new MY_DATA_STRUCTURE; //do stuff to foo } Then in C# I call the function thus: [DllImport("MyDll.dll")] static extern void AllocateFoo(out IntPtr pMyDataStruct); ... MyDataStructure GetMyDataStructure() { IntPtr p...

unmanaged/managed interop - trouble passing int[]

Hello, I am working on my phd in chemistry and for that reason I need to write a software application to help me with the imaging of samples under a microscope. This microscope is fitted with an x-y-z nanopositioning stage. The stage is controlled using an unmanaged DLL written in VC++ by the hardware vendor. I could provide you with mo...

How to marshal unmanaged buffer of packed structs in c#

Hello I am (successfully) calling the Windows FilterSendMessage function in c# using the following pinvoke signature: [DllImport("fltlib.dll")] public static extern IntPtr FilterSendMessage( IntPtr hPort, IntPtr inBuffer, UInt32 inBufferSize, IntPtr outBuffer, UInt32 outBufferSize, ou...

How to pass an interface pointer to a thread?

Note: Using raw Win32 CreateTheard() API No MFC An interface is simply a pointer to a vtable Question: How to pass an interface pointer to a thread? Illustration: IS8Simulation *pis8 = NULL; ... CoCreateInstance( clsid, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IS8S...

C++ CLI cryptic function call

So I'm trying to call a function that is a manged wrapper around an OCX object. Having great difficulty. Function is; foo(System::Object ^% theBuffer) where 'theBuffer' is an array of bytes. The template for the unmanaged OCX which 'foo' wraps is goo(VARIANT* theBuffer); So I've tried; System::Int32 buf[10]; foo(buf); which fail...

Create SecureString from unmanaged unicode string

I am wanting to try to tie the CryptUnprotectData windows API function and the .net SecureString together the best way possible. CryptUnprotectData returns a DATA_BLOB structure consisting of an array of bytes and a byte length. In my program this will be a Unicode UTF-16 string. SecureString has a constructor which takes a char* and ...

How do you serialize an object in C++?

I have a small hierarchy of objects that I need to serialize and transmit via a socket connection. I need to both serialize the object, then deserialize it based on what type it is. Is there an easy way to do this in C++ (as there is in Java)? Are there any C++ serialization online code samples or tutorials? EDIT: Just to be clear, I...

Does MessageBox.Show() automatically marshall to the UI Thread ?

Hi, I launch a thread via ThreadPool.QueueUserWorkItem which has a messagebox dialogue in it: System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("would you like to download upgrade in background? ..", "Upgrade Available", MessageBoxButtons.YesNo); It seems to work fine however I am a little suspicious after so...

Pass more than one argument to CreateThread

Question: How to pass specifically two arguments to CreateThread, when: Argument one, of type SOCKET Argument two, an interface pointer: _COM_SMARTPTR_TYPEDEF(Range, __uuidof(Range)); RangePtr pRange; //pass pRange Suggestions: For interface pointer, using CoMarshalInterThreadInterfaceInStream, accordingly, ...

Equivalent to Marshal.PtrToString* methods for byte array

If I have an unmanaged pointer and I want to transfer a specified number of bytes from that location to a Byte array, what is the best way to do this? i.e. what is the equivalent of Marshal.PtrToString* methods but where the destination is a Byte() Thanks ...

may COM server reallocate ([in, out] CACLSID * arg) ?

With a COM interface method declared as this: [ object, uuid(....), ] interface IFoo : IUnknown { HRESULT Foo([in, out] CACLSID * items); } With regards to marshalling, is the server allowed to reallocate the counted array? (I think it is, but I am not sure anymore) Its current implementation only replaces the existing ID's, but...

Marshalling an an unmanaged array of strings from a PInvoked OpenFileDialog (GetOpenFileName)

OpenFileDialog returns a pointer to memory containing a sequence of null-terminated strings, followed by final null to indicate the end of the array. This is how I'm getting C# strings back from the unmanaged pointer, but I'm sure there must be a safer, more elegant way. IntPtr unmanagedPtr = // start of the array ... ...

Calling C++ dll function from C#: Of structs, strings and wchar_t arrays.

Here's a simple problem I need to solve, but it makes me feel my hair turning gray as all my attempts are returning me the same error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I have a sample app written in C++ which makes a call to the dll. Here is the relevant code: ...

Json <-> Java serialization that works with GWT

I am looking for a simple Json (de)serializer for Java that might work with GWT. I have googled a bit and found some solutions that either require annotate every member or define useless interfaces. Quite a boring. Why don't we have something really simple like class MyBean { ... } new GoodSerializer().makeString(new MyBean()); new...

How to convert/call a c++ project to/from .Net?

Hi, I currently have a huge project that I've recently converted from VC6 to 2005. Now I'd really like to create a new frontend for some of the functionality, However the main logic of the program is based in c++. Also the code base revolves around it's own metatypes and bespoke classes. The best solution I can come up with is to call ...