pinvoke

Best practices for organizing .NET P/Invoke code to Win32 APIs

I am refactoring a large and complicated code base in .NET that makes heavy use of P/Invoke to Win32 APIs. The structure of the project is not the greatest and I am finding DllImport statements all over the place, very often duplicated for the same function, and also declared in a variety of ways: The import directives and methods are s...

How to show printer properties/preferences dialog and save changes?

EDIT: My fault! I expected the changes to be written back to the default printer settings when in fact only the local instance of the PrinterSettings are changed. - The below code seems to work as intended I am trying to show the custom printer properties of a given printer. I need this as part of a custom PrintDialog which I am trying ...

How to utilize the Windows Vista/7 Dialogs?

I have noticed that the Windows Vista/7 dialogs look MUCH better than just a MessageBox. How can I use them with a P/Invoke? ...

How can i use a C# dll in a Win32 C++ project ?

I am working on a solution, most of its core engine is developed as Win32 C++ (and is Platform independent and is also used on OS X), some time ago we needed to call C++ dll's of core engine from C# and I was able to Load main solution's DLL in C# (by the help of some threads here on SO). but now we have certain things implemented in Man...

P/Invoke on 32 bit and 64 bit systems

Let us pick the following Win API call as an example: BOOL MessageBeep(UINT uType); // from User32.dll The input parameter is UINT to specify the beep type, which is can be both 32bit and 64bit integer, depending on which Windows version we call it on (or am I wrong?). If I want to P/Invoke message beep from C#, so I apply the DllImp...

Is it possible for an object to "pin" itself and avoid garbage collection?

I'm trying to write a drop-in replacement for System.Media.SoundPlayer using the waveOut... API. This API makes a callback to a method in my version of SoundPlayer when the file/stream passed it has completed playback. If I create a form-scoped instance of my SoundPlayer and play something, everything works fine because the form keeps ...

Get selected items from explorer using C# .NET 3.5

I am writing a .NET 3.5 WPF application in C#. This application needs to be able to get the selected items out of Windows explorer when it is in the foreground. I already have the code working that handles a global Windows hotkey and then checks to see if the foreground IntPtr is from explorer. If so, I am able to obtain the System.Diag...

Why does GetWindowThreadProcessId return 0 when called from a service

When using the following class in a console application, and having at least one instance of Notepad running, GetWindowThreadProcessId correctly returns a non-zero thread id. However, if the same code is included in a Windows Service, GetWindowThreadProcessId always returns 0 and no exceptions are thrown. Changing the user the service...

Cannot call DLL import entry, C# -> C++, EntryPointNotFoundException

I'm trying to call from C# a function in a custom DLL written in C++. However I'm getting the warning during code analysis and the error at runtime: Warning: CA1400 : Microsoft.Interoperability : Correct the declaration of 'SafeNativeMethods.SetHook()' so that it correctly points to an existing entry point in 'wi.dll'. The ...

Using pinvoke in c# to call sprintf and friends on 64-bit

I am having an interesting problem with using pinvoke in C# to call _snwprintf. It works for integer types, but not for floating point numbers. This is on 64-bit Windows, it works fine on 32-bit. My code is below, please keep in mind that this is a contrived example to show the behavior I am seeing. class Program { [DllImport("m...

IContextMenu::GetCommandString Not showing help text in Windows Explorer

Hi, i am implementing a shell context menu for windows explorer and have successfully created the menu's. What i am having trouble with is the IContextMenu::GetCommandString method that displays the help text in the status bar when you hover over the selected menu item. When i do hover over each item nothing is displayed, but whats weir...

Is it possible to get the name of current active application

User can switch active application by Alt+Tab or by clicking on their icons in TaskBar. Is it possible to get the name (or other unique characteristic) of current active application? I want to write a program which collects statistic of the applications usage. ...

Access C global variable 'errno' from C#

Is it possible to access the "errno" variable in C# when P/Invoking? This is similar to Win32 GetLastError(). ...

ShSetFolderPath works on win7, doesn't on XP

Hey. I'm trying to use ShSetFolderPath function in C#. I work on Win7, I've managed to use ShSetKnownFolderPath and it works fine. Since this function is unavaible in WinXP, i tried to invoke ShSetFolderPath. Because i'm not familiar with invoking, I've done some searching and found something on some French forum. I don't speak French, b...

SuppressUnmanagedCodeSecurity At Class Level

If I add the [SuppressUnmanagedCodeSecurity()] attribute to a class, is it equivalent to adding it to each function in the class? ...

Blittable Vs. Non-Blittable in IL

I'm trying to make sure that my Managed to Unmanaged calls are optimized. Is there a quick way to see by looking at the IL if any non-blittable types have accidentally gotten into my pinvoke calls? I tried just writing two unmanaged functions in a .dll, one that uses bool (which is non-blittable) and one that uses ints. But I didn't s...

Blittable Value Types

Here is a list of blittable types. It contains Int32 and Int64. But I don't see just plain "int" on the list. How does C# treat the plain "int" type? Does it just get replaced with Int32 or Int64 depending on the system? Or is there a subtle difference? Will using "int" ever cause a performance hit for marshalling? ...

Open "Folder Options" dialog programmatically

Exists any way to open "Folder Options" (In windows Explorer: Tools > Folder Options) dialog programmatically? If no, how to set "Show hidden files and folders"? Sorry for poor english. ...

Problem getting correct parameters for C# P/Invoke call to C++ dll

Trying to Interop a functionality from the Outside In API from Oracle. Have the following function: SCCERR EXOpenExport {VTHDOC hDoc, VTDWORD dwOutputId, VTDWORD dwSpecType, VTLPVOID pSpec, VTDWORD dwFlags, VTSYSPARAM dwReserved, VTLPVOID pCallbackFunc, VTSYSPARAM dwCallbackData, VTLPHEXPORT phExport); From the header files I redu...

Managed .NET Equivalent to CreateFile & WriteFile from WinBase (kernel32.dll)

I am working with a legacy file format. The file is created using unmanaged C++ that utilizes the WinBase.h CreateFile() & WriteFile() functions (found in the kernel32.dll). I have been using P/Invoke interop to access these native functions like so: [DllImport("kernel32.dll")] public static extern bool WriteFile( In...