pinvoke

How to write the C# prototype to P/Invoke a function which takes a char* as an argument, and returns its value there.

As the title suggests, I'm trying to write some C# code to interop with a C DLL (It's a Windows device driver, basically). The error code function signature in C is: UINT DLLErrorMsg( DWORD errorCode, char * pBuf, UINT nSize ); nSize is the size of pBuf. If nSize can fit the error message, it is copied into pBuf and 0 returned, othe...

need help custom marshaling..??

Hi, i need help about custom marshaling..i have a native DLL..the parameters of the some DLL functions is defined with unsigned char* for example; typedef short apiStatus; apiStatus __declspec(dllexport) __stdcall DrfGetFirmwareVersion (HANDLE hCom, unsigned char *major,unsigned char *minor, unsigned char ReaderAddr = 0xff); t...

Why does the .NET framework not include USB tools

I am wondering why the .NET framework does not provide any managed methods for working with USB drivers. From everything I have read you still have to call into Windows API's and use marshalling and PInvoke and all that crap that none of us .NET programmers want to have to do. Why does .NET provide methods for communicating with serial ...

Getting stdout when p-invoking to unmanaged DLL?

I've got an unmanaged DLL that is writing log messages to standard output. I'm calling this DLL with P-invokes from a WPF app and I need to get the standard output stream log. I've tried Console.SetOut, but that only seems to capture information written using Console.Write, etc. Anyone have any ideas? I've found similar questions asked ...

When passing a managed byte[] array through PInvoke to be filled in by Win32, does it need to be pinned?

Suppose you're calling a Win32 function that will fill in your byte array. You create an array of size 32, empty. Then pass it in to the Win32 function to be filled int, and use it later in your managed code. Does there exist the chance that the byte array might be moved or overwritten in between the time it was allocated and it is fille...

Help me convert C++ structure into C#

Hello, I am completely new to C#, and need help converting a C++ structure to C#. The C++ structure is given as: #define QUE_ADDR_BUF_LENGTH 50 #define QUE_POST_BUF_LENGTH 11 typedef struct { const WCHAR *streetAddress; const WCHAR *city; const WCHAR *state; const WCHAR *country; const WCHAR *postalCode; } QueS...

CeGetSystemInfo doesn't fill the struct passed by reference

I'm trying to get information about a Windows Mobile device from a desktop application (written in C#). I searched the MSDN and found that the function I need is in rapi.dll: VOID CeGetSystemInfo (LPSYSTEM_INFO lpSystemInfo); The parameter is a pointer to a struct which is deffined like this: typedef struct _SYSTEM_INFO { union {...

C# class representing a USB device

I'm writing a wrapper class in C# for a USB device (using P/Invoke to communicate with it). The device needs to be sent an open message before use, and a close method when it's done with. In my constructor I want to try to open the device and return null if that call fails for whatever reason. I'll also write a Dispose() which calls the ...

Using DirectX DLL (C++) from C#

I have a native C++ DLL using DirectX, that I want to be able to use through C# to be able to create content creation tools. Adding COM would require quite an effort it seems. Can P/Invoke be used to maintain classes using polymorphism, or would it require me to wrap most of the code to facilitate use of P/Invoke? It there a better sol...

How to pass a pointer to a struct via Pinvoke?

I am trying to write the C# equivalent to the following: typedef struct BATT_ID { UINT8 nBattID[8]; } BATT_ID, *PBATT_ID; HANDLE g_hDevice; // Connect to the driver g_hDevice = CreateFile(L"BAT1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); void GetB...

SetClassLong(hWnd, GCL_HICON, hIcon) cannot replace WinForms Form.Icon

I'd like to use a specific ICO file as my icon with a WinForms application. Since I want to be able to specify a small icon (16x16) for the title bar and a normal icon (32x32) when Alt-Tabbing, I cannot use the Form.Icon property which accepts a single System.Drawing.Icon object, which forces me to use either the low res icon, or the nor...

How are Win32 types represented in C# P/Invoke?

I am trying to call the PrintDlgW Win32 API from C# via P/Invoke. With the help of P/Invoke Interop Assistant 1.0 from Microsoft, I am able to declare necessary data structures and import functions from DLL. But how to use the HDC or HWND in C#? Thanks in advance. ...

Strange Last Error in P/Invoke

Hi,guys! I am calling "PrintDlg" Win32 API via P/Invoke. Strange enough, if I use Visual Studio debug mode (press F5) to run my application, the last error is 122, which means "The data area passed to a system call is too small" , but if i ran it with Ctrl+F5, the last error is 0 and the message is "The operation completed successfully"...

Check if a DLL is present in the system

Hi, quick question. I want to find out if a DLL is present in the system where my application is executing. Is this possible in C#? (in a way that would work on ALL Windows OS?) For DLL i mean a non-.NET classic dll (a Win32 dll) (Basically I want to make a check cause I'm using a DLL that may or may not be present on the user system,...

Using DrawThemeTextEx in C#?

I am creating a label control which can be used on Aero Glass and I want the text to glow "the Vista way". There is a WinAPI function called 'DrawThemeTextEx' but I don't know how to use it and searching didn't give many results. I found this message but it doesn't tell you much : http://www.codeproject.com/Messages/3273238/Text-on-Aer...

BitBlt code not working

I'm trying to use this code to draw a Bitmap directly onto a PictureBox: Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Users\Ken\Desktop\Load2.bmp"); Graphics grDest = Graphics.FromHwnd(pictureBox1.Handle); Graphics grSrc = Graphics.FromImage(bmp); IntPtr hdcDest = grDest.GetHdc(); IntPtr hdcSrc = grSrc.GetHdc(); BitBlt(hdcDest, 0, 0, pictu...

PInvokeStackImbalance when including a C DLL into C#

I have written a C DLL and some C# code to test including this DLL and executing functions from it. I am not too familiar with this process, and am receiving a PInvokeStackImbalance exception whenever my DLL function is called from the C# source code. The code is as follows (I have commented most code out to isolate this problem): C# In...

Correct way (in .NET) to switch the focus to another application.

This is what I have so far: Dim bProcess = Process.GetProcessesByName("By").FirstOrDefault If bProcess IsNot Nothing Then SwitchToThisWindow(bProcess.MainWindowHandle, True) Else Process.Start("C:\Program Files\B\B.exe") End If It has two problems. Some people have told me that SwitchToThisWindow is d...

WIn32 Help with SwitchToThisWindow, ShowWindow, and SetActiveWindow

I have a VB 6 MDI application. It responds to the depricated SwitchToThisWindow function, but not the ShowWindow and SetActiveWindow functions. I know ShowWindow and SetActiveWindow are declared correctly because I can use them with any other application. EDIT: My goal isn't to use these functions, it is simple to switch the focus from ...

waveout sometimes memory issue

This code works to output a piano tone for 2 seconds using winmm.dll via platform invocation services, it seems to work fine on XP but waveoutopen fails in windows 7 rc updated based on feedback from John Knoeller Imports System.Runtime.InteropServices Public Class AudioStream <StructLayout(LayoutKind.Sequential)> _ Private Structure ...