pinvoke

Having a hard time pinvoking with a byte array.

I have an unmanaged method in a DLL with the following signature: extern "C" ErrorCodeEnum BuildSerialTriggerSetup(TriggerSetup_type sSetup, BYTE * pFPGACfgStream, UINT32 * piStreamLen) I have a pinvoke set up in C# with the following signature: [System.Runtime.InteropServices.DllImportAttribute("SerTrigLib.dll", EntryPoint = "BuildS...

What is the correct C# interop code to handle this structure and method?

I am writing a managed wrapper around an unmanaged DLL, and am unable to get the following method and structure to marshal correctly. Here's what's in the .h file for the unmanaged code: typedef struct { WORD PI_code; DWORD grpStat[31]; BYTE PTY_code; char* PS; char* RT; } RdsData_t; COMPANYNAME_API void COMPANY_get_rds_data(Rds...

Where can I learn more about P/Invoke?

Lately, I've been doing a lot of interaction with unmanaged libraries and I keep coming back to SO to ask questions about certain method signatures because I'm not a C/C++ programmer (although it's not completely alien to me). There are situations where the same type of argument in two different methods require two different P/Invoke sig...

Add offset to IntPtr

Hello, I'm looking for a way to perform pointer operations in C# or .NET in particular. I want to do something very simple Having a pointer IntPtr I want to get IntPtr object which points to 2 bytes ahead. I read some post that the foolowing snippet will work... IntPtr ptr = new IntPtr(oldptr.ToInt32() + 2); But I have doubts whet...

Mono Resources for Cross Platform Development

Is there a site or repository of information about Cross Platform development in mono? Obviously I know about Moma, and the Cross Platform Guidlines and I am using the Visual Studio Tools to run and debug applications in a Virtual Machine, however since I am migrating an old application I have dozens (if not hundreds) of PInvokes to mig...

How can C++/CLI make this situation easier?

I have a little piece of a library that I would like to write a .NET wrapper for. At the moment, I'm using P/Invoke, but since I don't have the library's source code or a whole lot of C knowledge, I'm having a difficult time with marshaling. It works so far (sort of), but it feels like a hack. C Signatures typedef struct { unsigned...

Struct array never receives items, but the method says it did

Yet another one of my idiotic P/Invoke questions... Sometimes I think this stuff is going to be a piece of cake, but it blows up in my face. I have a simple unmanaged method that takes a destination array and fills it. unsigned short NvtlCommon_GetAvailableDevices(SdkHandle session, DeviceDetail * pDev_list, unsigned long * dev_l...

Uniformly handling error codes in an unmanaged API

I'm writing a wrapper around a fairly large unmanaged API. Almost every imported method returns a common error code when it fails. For now, I'm doing this: ErrorCode result = Api.Method(); if (result != ErrorCode.SUCCESS) { throw Helper.ErrorToException(result); } This works fine. The problem is, I have so many unmanaged method ca...

How to relate WAVE_MAPPER audio line with its audio device

Hello fellow programmers, I'm developing an application that among other things, enumerates all input audio devices (using SetupAPI) and then for each audio device, it lists all input audio lines (using winmm.dll). Basically, the way I'm relating the two is getting the device path from the audio device and then using waveInMessage to ...

Why can't I get GetPrivateProfileString to work via pinvoke?

Running a c# console app I wrote on 64 bit Vista. Here's the code: class Class1 { static void Main(string[] args) { Debug.Assert(File.Exists(@"c:\test.ini")); StringBuilder sb = new StringBuilder(500); uint res = GetPrivateProfileString("AppName", "KeyName", "", sb, sb.Capacity, @"c:\test.ini"); C...

PInvoke returns C type with union

How would I P/Invoke a C function which returns a union'ed struct? ...

Select Item from winlist with SendMessage

Hi how can I change Item state in windows forms list using send message function (C#) I saw a couple of examples on the internet but none was really successful in doing it. ...

PInvoke - crypt32.dll - CertStrToName and CertCreateSelfSignCertificate - how to set the issuer name and password

We currently use self-signed server certificates in our Windows-to-WCF application. The certificates are created with the following commands in a batch file: makecert -sv CERTNAME.pvk -n "CN=SUBJECTNAME" CERTNAME.cer -sky exchange cert2spc CERTNAME.cer CERTNAME.spc pvk2pfx -pvk CERTNAME.pvk -spc CERTNAME.spc -pfx CERTNAME.pfx I have b...

deviceiocontrol and disk size question

I am using C# and pinvoke to read/write a raw sd card. I need to get the total capacity with deviceiocontrol: [StructLayout(LayoutKind.Sequential)] public struct DISK_GEOMETRY { public long Cylinders; public int MediaType; public int TracksPerCylinder; public int SectorsPerTrack; public int BytesPerSector; public long DiskSize { get { r...

Hide all windows for a process (ex. Internet Explorer)

I am using WatiN to script actions in Internet Explorer that run in the background of the user's machine. (I know...hacky). I am able through WatiN to make the Internet Explorer window not show. However, during my script, some HTML dialogs are opened. I am able to hide them using the P-Invoke ShowWindow in User32.dll, but they still ...

Pointer to function for unmanaged code in C#

Hi, I have a dll which accepts a struct that contains a pointer to a function to do a callback. How can I get an IntPtr to a function of my application to build the struct? [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class OPERATION { public uint OperationID; public IntPtr Context; ...

C# P/Invoke: Marshalling structures containing function pointers

Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke internals better than I do. Here is how I'm marshalling structures containing function pointers from C to C#. I would like to know whether it's the cleanest and/or most efficient way of doing it. I'm interfacing with a native DLL coded in C th...

Specifying codepage for PInvoke string marshalling using C#

I am calling a DLL using PInvoke. The DLL's function returns a C string in codepage 437. Is there a way to have the .Net marshaling convert the string to unicode, or could someone suggest which parameters I should give to DllImport() and MarshalAs() and a conversion function to use in order to get an output in unicode? For reference, t...

C# and native overlapped I/O

I have a C dll that I'm wrapping so that I can call it from C#. One function uses events to notify you when a state has changed, and figuring out to deal with it took some digging. It seems to be working ok, but I'm curious if anyone has more experience with this than I do and can offer any advice. The function is defined in the dll's ...

marshalling a struct containing string

i basically want to take int name and string age from user in c# and send it to dll method written in c which take int and char[50] arguments in it and return string .i created following scenario but i am failed ,any body has the code i have a dll developed in c which ahas a structure struct Argument { int age; char name[50]; } ; ...