pinvoke

Getting Access Violation when call vc++ dll from Vb.net

The error is " AccessViolationException: Attempted to read or write protected memory " vc++ DLL #ifndef IZZIXFELAPI32_H #define IZZIXFELAPI32_H #ifdef IZZIXFELAPI32_EXPORTS #define IZZIXFELAPI32_API __declspec(dllexport) #else #define IZZIXFELAPI32_API __declspec(dllimport) #endif #define MAX_FEATUREVECT_LEN 480 #define HIGH_LEVEL ...

C# PInvoke out strings declaration

In C# PInvoke, how do I pass a string buffer so that the C DLL fills it and returns? What will be the PInvoke declaration? The C function declaration is int GetData(char* data, int buflength); In C#, I have declared it as [DllImport(DllName)] static extern Int32 GetData([MarshalAs(UnmanagedType.LPStr)]StringBuilder receiveddata, In...

"An attempt was made to load a program with an incorrect format" even when the platforms are the same.

I'm calling functions from a 32-bit unmanaged DLL on a 64-bit system. What I get is: BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) At first, I had my projects set to the Any CPU platform, so I changed them both to x86, but this error is still occurring....

C#: Marshalling a "pointer to an int array" from a SendMessage() lParam

I'm trying to subclass an unmanaged statusbar window from my managed COM server using a class inherited from NativeWindow, and am running into a wall trying to make sense of how to properly marshal the contents of an lParam. http://msdn.microsoft.com/en-us/library/bb760757%28VS.85%29.aspx says that the contents of this lParam is of type...

How to find what executable the user has just started?

I'm working on a testing framework that needs to be able to record a user's activities and then replay them. I'm fine using the ManagedWinAPI wrappers around P/Invoke ( working in C# ) to record mouse and keyboard activity, which works but I think that in order to make the recording more useful I need to know more about what happens when...

Managed and unmanaged struct are not the same size

I'm working with an unmanaged library through P/Invoke and it uses three structs (although they all have the same basic layout, so I'll only post one): struct Agraph_t { int tag:4; int kind:4; int handle:24; char **attr; char *didset; char *name; Agdata_t *univ; Dict_t *nodes, *inedges, *outedges; Agr...

How to get GetFunctionPointerForDelegate for method with SafeHandle or workaround

This is code example which causes MarshalDirectiveException. Good explanation of SafeHandles could be found here. [SuppressUnmanagedCodeSecurity] private delegate SafeHandle testDelegate(); [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public static SafeHandle test(){ FileStre...

Nested structures in C# P/Invoke

I am trying to call into an unmanaged DLL that has the following structure: typedef struct { int num_objects; ppr_object_type *objects; } ppr_object_list_type; ppr_coordinate_type; typedef struct { int model_id; ppr_coordinate_type position; float scale_factor; float size; ppr_rotation_type rotation; int...

Add a Reference from a C# App to a DLL compiled without /clr?

I'm using Visual Studio 2008 to build a Solution with two Projects: a C# Console App and a C++ DLL. I want the app to call a function from the dll using P/Invoke. Therefore I'm trying to add the dll as a Reference to the C# app. But when I try the Add Reference command, Visual Studio won't let me do it unless I set the /clr property on t...

How to make a PInvoke friendly native-API?

How to make a native API to be PInvoke friendly? there are some tips on how to modify native-programs to be used with P/Invoke here. But before I even write a native programs, what are the things I should look out to make my programs/library PInvoke friendly? using C or C++ are fine. update: if I write a C API, what are the things I h...

How to control windows application focus in .NET

I'm using pinvoke "user32.dll" to send my application to back (behind all other apps) so it sits on desktop and vice versa. At the moment it just toggles - back/front. Is there a way to detect if my app is at the back and bring it to front or if it's on front and send it to back? THanks. ...

What is the proper PInvoke signature for a function that takes var args?

There is a native function: int sqlite3_config(int, ...); I would like to PInvoke to this function. Currently, I have this declaration: [DllImport("sqlite3", EntryPoint = "sqlite3_config")] public static extern Result Config (ConfigOption option); (Result and ConfigOption are enums of the form enum Result : int { ... }.) I am actu...

.NET wrapper for Windows API functionality

Does anyone know of a .NET managed wrapper around Windows API functionality that is not available in the .NET framework itself? Areas such as window creation and display styles, common UI control manipulation, keyboard/mouse input, file and disk information, memory mapped files etc I have been a regular to visitor http://www.pinvoke...

How to free IntPtr in C#?

How to free ptrSentFromPinvokedDLL? IntPtr ptrSentFromPinvokedDLL= IntPtr.Zero; int resultFromVendor = CallVendorDll(ref ptrSentFromPinvokedDLL); resultFromVendor = DoMoreWorkFromVendorDLL( ptrSentFromPinvokedDLL, "workonthis"); // Free ptrSentFromPinvokedDLLhere Thanks in advance ...

Multi-Threading Pinvoked DLLs in managed environment. Is it possible?

I have a Vendor.DLL (Native DLL, written in C++) that exposes many methods. Typically Vendor.DLL opens Vendor proprietary files, returns handles and allows more Read/Write operation on those files. Vendor.DLL supports multi-threading (when called from unmanaged code/COM). If I expose Pinvoked method(s) from Vendor DLL, say PinvokedVen...

Access violation where there wasn't one before

I'm P/Invoking out to Graphviz as shown here. When I wrote that blog entry, the code worked just fine. Now, I'm putting together an HttpModule that renders Graphviz graphs using that code, but I get an AccessViolationException at agmemread. // Native signature Agraph_t agmemread(char *); // P/Invoke Signature [DllImport(LIB_GRAPH)] pri...

Marshaling a struct while keeping it "unmanaged"

I'm p-invoking into a DLL that returns a void** list of struct pointers, all of the same type. From what I've read, in order to cast and get my structure out of that list, the struct needs to be considered unmanaged. The main culprits to the struct I'm trying to marshal over are the following two fields from the C side: char name[1024];...

sendmessage (pinvoke) keeps crashing.

I am having to write an application that communicates with a third party program (AOL, I'm sorry. :() Doing a lot of research I found some ways to do this with PInvoke, and for the most part it works okay, but it crashes upon subsequent trials. Specifically with SendMessage. I'm outlining the crashing code below - I can post more if any...

detecting window text change with pinvoke

Is there any way to see if the contents of a 'window' have changed, that is referenced only by a handle? Such as one obtained from a pinvoke FindWindow? ...

How to call unmanaged c++ function that allocates output buffer to return data in c#?

I have problems with marshalling output parameter of c++ function returning array of data to c#. Here is C++ declaration: #define DLL_API __declspec(dllexport) typedef TPARAMETER_DATA { char *parameter; int size; } PARAMETER_DATA; int DLL_API GetParameters(PARAMETER_DATA *outputData); The function allocates memo...