pinvoke

Scrollbar flicker when calling EnableScrollBar

I'm using p/invoke to call EnableScrollBar from user32.dll (MSDN reference). I noticed that when the scrollbar is enabled, it seems to draw as though no theme is applied and then re-drawn with the theme applied. I've only tested with Windows 7 so far. Is there any way to stop this from happening? EDIT: Here's some code to show what hap...

VisualStudio unit test doesn't find my DLL with P/Invoke. How can I fix this?

Hello, I'm working on Windows 7 with Visual Studio 2008. I have a .NET assembly that makes calls into a native DLL with P/Invoke. I have set up a separate .NET unit test project in my Visual Studio solution that tests the assembly by making various calls into it. However, when the unit test makes a call into the assembly, and the ass...

NUnit unit test cannot find a unmanaged dll in the test class

I'have the following situation: I have a delphi dll (unmanaged) that works. I have a win forms application (a proof of concept application) that works. The dll (and all its dependencies) are copied in the Bin/Debug directory of the application. I've createad a .NET managed library which has a class that is a wrapper around the dll. all...

Marshalling of C structure to C#

Please bear with me as i am new to marshalling. I have a C structure and function declared as the following: typedef struct { char* name; BT_ADDR address; } DeviceList; extern "C" _declspec(dllexport)DeviceList* PerformQuery(); The BT_ADDR structure is the same structure defined in wsbth2.h in Win CE SDK. PerformQuery return...

doing pInvoke and can't get the right hDC

So I'm trying to get the right Device Context so I can set the gamma ramp on individual monitors in an N monitor configuration (2+). I've tried [DllImport("gdi32.dll")] static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData); With this method I'm using the string "DISPLAY" for lpszDr...

Impact of Microsoft Security Advisory (2269637) on .NET coding

Microsoft released Security Advisory (2269637) Insecure Library Loading Could Allow Remote Code Execution. The note refers to a tool that will help to detect this problem and programming guidelines on Dynamic-Link Library Security. How do these guidelines translate to .NET development? I assume this only affects Platform Invoke. Does...

Help using PInvoke CreateDirectory () in C#

I'm using C# for WinForms app in VS2010 and I needed to create a directory in which the path was too large for the .NET methods (248 char limit, I believe) and ran across suggestions from google to use the Unicode Win32 CreateDirectory(). I had initially tried calling it using Unicode and passed parameters but after several failed attem...

How to map Win32 types to C# types when using P/Invoke?

I am trying to do something like this in C#. I found out how to call Win32 methods from C# using P/Invoke from this link. However I met some difficulties in implementing P/Invoke. For example, one of the methods that I would like to access is PdhOpenQuery, signature: PDH_STATUS PdhOpenQuery( __in LPCTSTR szDataSource, __in DWO...

LookupAccountSid taking +600 ms per call

I'm using GetTokenInformation with the TokenGroups flags to retrieve all the groups that a particular token is part of. I'm then looping through each of the returned SIDs and using LookupAccountSid to grab the user name and domain. I call LookupAccountSid twice: the first time to grab the size of the name and domain character arrays and ...

UnmanagedCode permission. What is it?

The following code exists in LogEntry.cs in the Enterprise Library's Logging Application Block: private bool UnmanagedCodePermissionAvailable { get { if (!unmanagedCodePermissionAvailableInitialized) { // check whether the unmanaged code permission is available to avoid three potential stack walks bool internalUn...

how to access defines and structs inside a .h from C#/.NET?

In accessing PDH.dll (a Win32 API) (see link) using P/Invoke, it turns out that I need access to several constants and structs. I found these are defined inside a file PDH.h. How do I access these defines and structs? Do I have to rewrite them with C#? Is there another way? ...

C# Free memory allocated by operator new from p/invoke DLL

Hi, I have a C# application which is using a third party (closed source) native DLL through p/invoke. During some of these calls, the DLL will allocate memory using operator new / operator new[] and return a pointer to use for reading the results. This memory is never freed by the DLL after being allocated and returned. How can I perfo...

Returning Struct from VC++ to C#.

Hi, I have written a structure in VC++. I have made a dll of the VC++ code and calling this dll in C# using PInvoke. The VC++ dll looks like this #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #include <iostream> #if defined(_MSC_VER) #include <windows.h> #define DLL extern "C" __declspec(dllexport) #els...

Access Violation with unmanaged DLL

Currently, we use an unmanaged DLL from a vendor that allows us to access a particular instrument. The particular function of interest is specified in a header file as this: extern "C" short CCONV acq_get_board_count (); In my application, I have the pinvoke statement: public class bograms { [DllImport("bograms.dll", EntryPoint =...

Will struct modifications in C# affect unmanaged memory?

My gut reaction is no, because managed and unmanaged memory are distinct, but I'm not sure if the .NET Framework is doing something with Marshaling behind the scenes. What I believe happens is: When getting a struct from my unmanaged DLL, it is the same as making that call gets an IntPtr and then uses it and the Marshal class to copy th...

P/invoke System.ExecutionEngineException when passing array as ref/out

Im using an P/invoke on an unmanaged dll function swe_calc_ut. int swe_calc_ut(double tjd_et, int ipl, int iflag, double *xx, char *serr) Parameter xx is meant to be an "array of 6 doubles for storing the result", and parameter serr a "character string to return error messages" my c# code is as follows. [DllImport("swedll32.dll")]...

pInvoke C# DLLimport problem

Im using P/invoke on an unmanaged dll function swe_get_planet_name() in C#. The given function definition is, char* swe_get_planet_name(int ipl, char *spname); This means the return value is assigned to char *spname? It seemed so from the sample code in the documentation. char snam[40]; /* * get the name of the planet p ...

Global hotkey in console application

Does anyone know how to use the RegisterHotKey/UnregisterHotKey API calls in a console application? I assume that setting up/removing the hotkey is the same, but how do I get the call back when the key was pressed? Every example I see is for Winforms, and uses protected override void WndProc(ref Message m){...}, which isn't available...

What are the disadvantages of using P/Invoke

I'm working already a good time with the .net framework. In this time there were some situations, I used P/Invoke for doing things that I couldn't do with managed code. However I never knew exactly what the real disadvantages of using it were. That was also the reason why I tried not to use it as far as possible. If I google, then I f...

NetUserGetInfo/NetLocalGroupGetInfo returning error 1722

I have the following code (VB.NET) which is designed to determine if a given account name refers to a local group or a user account. This will only be called for accounts/groups on a machine, not a domain. Module netapi Private Declare Function NetUserGetInfo Lib "Netapi32.dll" ( _ ByVal ServerName As String, _ By...