pinvoke

Is it possible to force a string to be a specific size when defining a struct?

I am marshalling data between a C# and C++ application. In the C# application, I force the size of a string to be some size (say, 256 bytes). I would like to read in that exact same amount in C++ (I will be recreating the structs with reinterpret_cast) so that the data will remain formatted as it was in the C# application. Unfortunate...

How to set up a C++ function so that it can be used by p/invoke?

Hopefully this is a brainlessly easy question, but it shows my lack of expertise with C++. I'm a C# programmer, and I've done extensive work with P/Invoke in the past with other people's C++/C dlls. However, this time I've decided to write a wrapper C++ dll (unmanaged) myself, and am then calling my wrapper dll from C#. The problem I ...

C# PInvoking user32.dll on a 64 bit system

Is it wrong to pinvoke user32.dll on 64 bit Windows, from a 64 bit app? I've done this successfully a number of times and never had an error, but it seems contradictory. Should I look for user64.dll instead? ...

How to call user32.dll methods from javascript

Hello all, I have a javascript running on a browser. Is it possible to call a function/method in user32.dll. This is possible from C# by using pInvoke calls. How do I do the same in JavaScript? Thanks, Datte ...

using dllimport

I'm trying to use the DllImport attribute in the following code: [DllImport("grfinger.dll",EntryPoint="_grstartenroll@4")] public static extern int startenroll(int context); to get the function name I used dumpbin /export. When I run the code I get the following exception: Unable to find an entry point name '_grstartenroll@4' in DLL ...

.NET PInvoke Exception Handling

What exceptions can occur when using PInvoke or are all errors handled by the method return values and it is up to the developer to check and raise exceptions if needed? ...

Stepping into a P/Invoke call in disassemby view

My C# code is calling an unmanaged third-party library function via P/Invoke, and the unmanaged function is having some strange side effects. I want to debug into it and see what it's doing. If I debug my C# code, and try to "Step Into" the P/Invoke call, it steps over instead. No surprise there -- I expected that; it doesn't have the s...

How do I read the names from a PE module's export table?

I've successfully read a PE header from an unmanaged module loaded into memory by another process. What I'd like to do now is read the names of this module's exports. Basically, this is what I have so far (I've left out a majority of the PE parsing code, because I already know it works): Extensions public static IntPtr Increment(this I...

Is it possible to restart a PC using "pure" .NET and *without* using p/invoke?

Is it possible, without employing pinvoke, to restart a PC using .NET? I kind of just repeated the title, but I'm not too sure how to elaborate much further! Edit: I should have mentioned that don't want to use "shutdown -r" as a solution. I was really after a pure .NET way, something like: Environment.ShutDown(); In other words, s...

CPU Architecture Independent P/Invoke: Can the DllName or path be "dynamic"?

Is there a way to have the particular DLL referenced by a P/Invoke (DllImport) signature depend on the CPU architecture? I'm working on an application that loads a large number of method signatures from a native dll from a third party vendor, in this case the user-space interface DLL to a piece of hardware. That vendor has now started ...

How can I pass a pointer to an integer in C#

I have a C API with the signature: int GetBuffer(char* buffer, int* maxSize) In C, I will call it this way: char buffer[4096]; int maxSize = 4096; GetBuffer(buffer, &maxSize); maxSize is set to the buffer size, and set with the actual size filled. I need to call it from C#. How do I do that under "safe mode"? Thanks ...

Detect Errored Devices on Windows XP with C#/pInvoke

How do you query the status of all detected devices? Where devices are USB connected hardware ( cameras, credit card readers ), PCI bus connected hardware, or hard drives. Anything that can show up as errored in the Device Manager is of interest. ...

Profiler for a C++ module in a C# app

I rewrote a number-crunching two pages of code from C# to unmanaged C++ in my project, which with full optimizations gave a 3x speedup. I want to keep optimizing that code, but now my profiler of choice, dotTrace, can't do it, because it only looks at managed code. How do I profile the P/Invoked C++ module when it's running in the C# ap...

How do I import this function from inet.dll into .NET?

I've never done this before, and I'm kind of stumped as to how I would translate the datatypes into C#. Here is the function I'm trying to import: BOOL InternetSetOption( __in HINTERNET hInternet, __in DWORD dwOption, __in LPVOID lpBuffer, __in DWORD dwBufferLength ); All I'm trying to do is set the proxy settings on a Web...

Help with PInvoke...

I got the next c function: long _stdcall _MakePipeString(char *szOut, long nOutChars, const char *szXmlFile, long nOptions); And I try to use pInvioke like this: [DllImport("diXo10.dll")] public static extern long _MakePipeString(out StringBuilder szOut, out long nOutChars, string szXmlFile, long nOptions); But i get null in the ou...

What am I doing wrong with this use of StructLayout( LayoutKind.Explicit ) when calling a PInvoke struct with union?

The following is a complete program. It works fine as long as you don't uncomment the '#define BROKEN' at the top. The break is due to a PInvoke failing to marshal a union correctly. The INPUT_RECORD structure in question has a number of substructures that might be used depending on the value in EventType. What I don't understand i...

What is the difference between structures containing bool vs uint when using PInvoke?

Ok, I'm now very confused. After my last question had several people comment about changing bool to uint I verified they are the same size by: Console.WriteLine("sizeof bool = {0}", Marshal.SizeOf(typeof(bool))); Console.WriteLine("sizeof uint = {0}", Marshal.SizeOf(typeof(uint))); Which of course prints: sizeof bool = 4 siz...

PInvoke - how to represent a field from a COM interface

I am referencing a COM structure that starts as follows: [scriptable, uuid(ae9e84b5-3e2d-457e-8fcd-5bbd2a8b832e)] interface nsICacheSession : nsISupports { /** * Expired entries will be doomed or evicted if this attribute is set to * true. If false, expired entries will be returned (useful for offline- * mode and cli...

PInvoke - reading the value of a string field - "Attempted to read or write protected memory"

I'm having trouble accessing the some string fields in a COM interface. Calling the integer fields does not result in an error. When attempting call clientID(), deviceID() or key(), I get the old "Attempted to read or write protected memory" error. Here is the source interface code: (code sourced from here) [scriptable, uuid(fab51c92-9...

Open/close connection to UNC without credentials

I have a .NET client app that intermittently loses connection to a UNC share where the user is either on a domain or has a local account with the same credentials on the server. Both SO and Google have plenty of examples using LogonUser and WNetAddConnection via P-Invoke, but both require the user's password. All our app needs to do is ...