pinvoke

Why this Explicit P/Invoke does not work ?

The following .net to native C code does not work, any ideas extern "C" { TRADITIONALDLL_API int TestStrRef( __inout char* c) { int rc = strlen(c); std::cout << "the input to TestStrRef is: >>" << c << "<<" ; c = "This is from the C code "; return rc; } } [DllImport("MyDll.dll", SetLastError = true)] static ext...

C#: How to use SHOpenFolderAndSelectItems

Could someone give an example on how to use the shell function SHOpenFolderAndSelectItems from C#? I don't quite get how to use these kind of functions and couldn't find it on pinvoke.net... =/ Say I have three files called X:\Pictures\a.jpg X:\Pictures\s.jpg X:\Pictures\d.jpg I then want to open up the X:\Pictures folder with a.jpg...

How to correctly hook and return GetDlgItemTextA from C++ to C# to C++ from EasyHook

I'm using EasyHook, a C# library for injecting and detouring functions from unmanaged applications. I'm trying to hook onto GetDlgItemTextA, which takes the arguments: UINT WINAPI GetDlgItemText( __in HWND hDlg, __in int nIDDlgItem, __out LPTSTR lpString, __in int nMaxCount );` In my hook, I am casting it as: [DllImpo...

Running DllImport commands from CreateRemoteThread

I have a function named Msg that's imported from a dll named tier0.dll. I can DllImport this just fine, but the command only works when the dll is attached to another process which can complete the Msg command. Using CreateRemoteThread, it is possible that I could call Msg using C# while still letting it have access to the variables of t...

pinvoke to clutter function

I'm trying to pinvoke to a clutter function. The function is defined in the docs as ClutterActor * clutter_texture_new_from_file (const gchar *filename, GError **error); The code I have is as follows: [DllImport ("libclutter-glx-1.0.so.0")] private static extern IntPtr clutter_texture_new_from_file (string filename, IntPtr errorData...

What's the "right" way to get Win32 p/Invoke declarations?

I typically use the site http://www.pinvoke.net/ to grab a DllImport declaration whenever I need to call a Win32 API, and I've noticed it's the de facto standard response on Stack Overflow to API interop questions. Is this what "everyone" does? Is there a better way? Does Microsoft offer an alternative? e.g. a tool that reads .h files a...

Loading a Win32 control in C# (specifically WPF)

I have written a set of Win32 dlls that encapsulate a Delphi Frame (see Snippet 1 below), and can load them into another Delphi program by loading the dll and assigning the right variables (Snippet 2). I now want to be able to do the same thing in C# (I can load the DLL in pinvoke, but am unsure how to connect up the control to the basic...

Feeding PDF through IInternetSession to WebBrowser control - Error

As related to my previous question, I have developed a temporary asynchronous pluggable protocol with the specific aim to be able to serve PDF documents directly to a WebBrowser control via a database. I need to do this because my limitations include not being able to access the disk other than IsolatedStorage; and a MemoryStream would b...

C# & C++, runtime error when call C++ dll from C#

I have written a C++ wrapper DLL for C# to call. The DLL was tested and worked fine with my C++ test program. now integrated with C#, I got runtime error and crashed. Cannot use debugger to see more details. The C++ side has only one method: #ifdef DLLWRAPPERWIN32_EXPORTS #define DLLWRAPPERWIN32_API __declspec(dllexport) #else #define...

Identifying if a user is in the local administrators group

My Problem I'm using PInvoked Windows API functions to verify if a user is part of the local administrators group. I'm utilizing GetCurrentProcess, OpenProcessToken, GetTokenInformationand LookupAccountSid to verify if the user is a local admin. GetTokenInformation returns a TOKEN_GROUPS struct with an array of SID_AND_ATTRIBUTES stru...

What kind of assemblies can be called using P/Invoke ?

Hi all, I allready asked at: Is it possible to call unmanaged code using C# reflection from managed code ? if it is possible to call C/C++ library unmanaged function with Invoke and reflection from .NET and the answer is yes. What I am not clear about is can I call using P/Invoke ANY assembly written/compiled/build with other compile...

How to supply a password to POWER_STATE_PASSWORD?

I am trying to get the powerstate for my windows mobile device. The states are defined as: #define POWER_STATE(f) ((f) & 0xFFFF0000) // power state mask #define POWER_STATE_ON (DWORD)(0x00010000) // on state #define POWER_STATE_OFF (DWORD)(0x00020000) // no power, full off #define POWER_STATE_CRITICAL ...

When inheriting from SafeHandle, should we reapply the ReliabilityContract attribute?

In the .Net security blog article on SafeHandles, it mentions that you need to apply the ReliabilityContract attribute to the signature of the native method that closes the handle. When we inherit from SafeHandle we have to declare a constructor, ReleaseHandle method and IsInvalid property, all of which have the ReliabilityContract appl...

C# and SendMessage (keys) is not working

Hi, I tried to send a key to an application. For an easy test I just used notepad. That's what the code looks like: [DllImport("USER32.DLL", EntryPoint = "SendMessageW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool S...

p/invoke a 32-bit dll from a C# program running on an x64 machine

I have a C# program that I compile with all of the default settings on an x64 computer. I want to p/invoke a DLL which I know is a 32-bit (unmanaged) C++ DLL. I can get this to work when my C# program runs on a 32-bit machine, but not a 64-bit machine. How can I specify in the DllImport call that I am calling into a 32-bit dll? Exam...

How to get the handle of a MainMenu object in Compact Framework

Using the compact framework (ie managed code), how can I get the handle to a MainMenu object? (P/Invoking is fine) ...

Using DLL import/Declare in VB.NET with parameter types not used by .NET

I am working on a project where I have to import a DLL file into a VB project that was created a few years back. The DLL was created in C++, and looks like: void CoordinateConversionService::convert( SourceOrTarget::Enum sourceDirection, SourceOrTarget::Enum targetDirection, CoordinateTuple* sourceCoordinates, Accuracy* sourceAccuracy, ...

P/Invoke: How to know which type to marshall from!?

Is there a one-stop shop for determining which .Net types/attributes to use, given a native type? Example would look something like this: Native Type | .Net Type --------------------------------------- int | Integer int* | IntPtr (or is it ref int?) LPCSTR | [MarshalAs(Un...

What is the difference between pInvoke and COM Interop?

Let us say that I am accessing a third-party library, for which the documentation states that I can use pInvoke or create an interop library and use COM. What is the difference between these two techniques, and why might I choose one over the other? ...

how to use C dll headers library in c#

Hello ; I am very new to C#, and i am trying to use a help package for my project. The package is written in c and has 1) /bin/ several .dll files 2) /include/ has a header file 3) /lib/msvc/ .lib file my question is how can i use those files in my C# WPF project? i know there is no "#include" in C#, and the .dll can not be imported ...