pinvoke

c# p/invoke difficulties marshalling pointers

I am attempting to call into a native .dll from c# using p/invoke. I'm able to make the call (no crash, the function returns a value), but the return code indicates "A pointer parameter does not point to accessible memory." I've resorted to trial and error in order to solve this, but I haven't made any progress so far. Here's the sig...

C# P/Invoke: How to achieve double indirection for a field of a structured parameter

I am calling into a native dll from C#. For the specific function in question, one of the parameters I need is a structure which contains a doubly-indirect field (pointer to a pointer). For example, take the following C prototype and structs: int someFunc(SomeStruct* result); struct SomeStruct { DWORD foo; AnotherStruct** p...

Is it possible to intercept WIN32 Exceptions for 3rd party applications?

Is it possible to intercept WIN32 exceptions for 3rd party applications? I have a particularly bad behaving 3rd party application for which I'm try to intercept unhandled exceptions so I can know that I need to kill the process but I'm finding it hard to find anything about this subject that seems to fit what I need. Any help would be...

Emulate Key Presses On an App that takes no Win Messages

I want to send an Application Key Presses, To Automate some stuff that has to be done repeatedly and So I don't always have to cramp my fingers. In C#, it's nice to use SendKeys.Send(), but this won't work because the Application doesn't take Windows Messages. SendKeys.SendWait() does nothing at all. How would I STILL Simulate the Keyb...

c#: generically convert unmanaged array to managed list

I am dealing with a set of native functions that return data through dynamically-allocated arrays. The functions take a reference pointer as input, then point it to the resulting array. For example: typedef struct result { //..Some Members..// } int extern WINAPI getInfo(result**); After the call, 'result' points to a null-termi...

Complicated (?) pinvoke situation - avoid function overloading

Summary: I have a bunch of C functions I have to call from C#. My current working solution is based on function overloading and I'm wondering if there is a more elegant solution. The C stuff: somewhere in a header file typedef struct _unknown_stuff * handle; // a opaque pointer an example of the function func( uint num_entri...

How to tell if .NET compiled code uses p/invoke?

What is the easiest way to tell if .NET compiled code is using p/invoke? ...

How to determine how long a song is using winmm.dll?

I've P/Invoked the mciSendString method from WinMM.dll: [DllImport("winmm.dll")] private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback); I can play, pause, and stop songs (I can also open/close the CD drive, but that's not im...

What is "ShowWindow Lib "user32" " about?

I was looking over some code that another developer wrote and found this: Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer What does it do and what is it for? ...

How do I call managed .NET code from my un-managed C++ code in Windows and vice versa?

I have a pure C++ application developed using VC 6.0. I would like this application to make use of a library developed in C#. How do I call the methods in the C# library from my native executable? I do not want to convert my un-managed C++ native application to managed code. Similarly, how do I do the reverse? Is PInvoke the only option?...

How to pass a nullable type to a P/invoked function

I have a few p/invoked functions (but I'm rewriting my code at the moment so I'm tidying up) and I want to know how to use/pass a nullable type as one of the parameters. working with int types isn't a problem but given the following: [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern IntPtr SetupD...

How can I check if a directory can be renamed in VB.NET?

My thought is to use CreateFile from kernel32 and check for sharing violations. I believe this will work because I watched the file system activity with Process Monitor while issuing a rename command from CMD that I knew would fail and the last activity was a failed CreateFile call that resulted in a sharing violation. This is the Proce...

P/Invoke Struct with Pointers, C++ from C#

I'm attempt to call a C++ dll with a struct and function like struct some_data{ int size,degree,df,order; double *x,*y,lambda; }; extern "C"{ __declspec(dllexport) double *some_func(some_data*); } from C#: [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] pu...

Calling DeviceIoControl from C# with IOCTL_DVD_* Control Codes

I am trying to call DeviceIoControl from C# for IOCTL_DVD_* control codes. Having read a lot of information and trying a number of examples I have not made much progress. What I am trying to eventually do is get a DVD_LAYER_DESCRIPTOR structure about the media currently in the DVD drive. I can call CreateFile successfully on the DVD d...

Installing native assembly into GAC

Hello, I have an assembly containing a mixture of Managed and unmanaged C++ code. I have signed it and installed into Global Assembly Cache. My program (.Net, C#) won't find it there, although it worked perfectly well when the assembly was in the program directory. The program uses DllImport and pinvoke to call the methods from the asse...

PInvoke of NetUserAdd returns 24

I am calling NetUserAdd and it is returning 24 for every call. Does anyone know what 24 stands for? It is kind of hard to debug it when I don't know what the error means. I am calling this from an Windows XP machine running as a local admin. I am also a local admin on the target computer. I tried this with USER_INFO_1 and it worked ...

How to use WM_Close in C#?

Can anyone provide me an example of how to use WM_CLOSE to close a small application like Notepad? ...

Need a way to change a remote user password - NetUserChangePassword fails with 2245

I am trying to call NetUserChangePassword to change the passwords on a remote computer. I am able to change the password when I log-in to the machine, but I can't do it via code. The return value is 2245 which equates to the Password Being too short. I read this link: http://support.microsoft.com/default.aspx?scid=kb;en-us;131226 but ...

Getting a key (secret) for a HMAC-MD5 hash

I am looking at the NetUserSetInfo method. It can take a USER_INFO_21 structure that allows me to pass in a "one-way encrypted LAN Manager 2.x-compatible password". I think this means a HMAC-MD5 hash. The class System.Security.Cryptography.HMACMD5 can create one of these hashes, but it needs a key (or shared secret) for that class to ...

C Dll import C#

How do you write a struct to a place in memory that will be able to be referenced via the ref call and NOT be changed. I've been passing with ref because I need a pointer to communicate with a dll and the values are getting changed. Not passing by ref throws a "Attempted to read or write protected memory" error. Thoughts? ...