pinvoke

Refreshing system tray icons programmatically

别来无恙 提交于 2019-12-21 04:49:19
问题 I've an application which has a system tray icon. While uninstalling I'm killing the process if its running. So, as am not gracefully stopping the app, the icon remains in the system tray and will remove only if we hover the mouse on it. I wrote a code that would run the cursor along the tray and get the cursor back in its initial position. This is what I have done: [DllImport("user32.dll")] static extern IntPtr FindWindow(string className, string windowName); [DllImport("user32.dll")] static

Call win32 CreateProfile() from C# managed code

好久不见. 提交于 2019-12-21 02:47:22
问题 Quick question (hopefully), how do I properly call the win32 function CreateProfile() from C# (managed code)? I have tried to devise a solution on my own with no avail. The syntax for CreateProfile() is: HRESULT WINAPI CreateProfile( __in LPCWSTR pszUserSid, __in LPCWSTR pszUserName, __out LPWSTR pszProfilePath, __in DWORD cchProfilePath ); The supporting documents can be found in the MSDN library. The code I have so far is posted below. DLL Import: [DllImport("userenv.dll", CharSet = CharSet

C#: Access 32-bit/64-bit DLL depending on platform

筅森魡賤 提交于 2019-12-21 02:33:13
问题 we use a self-written 32bit C++ DLL from our C# applications. Now we've noticed that when the C# applications are run on a 64bit system, the 64bit runtime is automatically used and of course the 32bit DLL can not be accessed from the 64bit runtime. My question is: is there a way of using the 32bit DLL? If not, if I created a 64bit version of the DLL, would it be easily possible to let the application choose which one to P/Invoke to? I'm thinking of creating two helper classes in C#: One that

What is the difference between a delegate instance and a method pointer?

浪子不回头ぞ 提交于 2019-12-20 20:21:38
问题 I thought that a delegate instance was interchangeable with a function instance. Take the following code: delegate int AddDelegate(int a, int b); AddDelegate DelegateInstance; public void DoStuff() { //I can call this without a delegate "instance": MethodThatTakesAdd(Add); //I can also call it WITH a delegate "instance" DelegateInstance = Add; MethodThatTakesAdd(DelegateInstance); } public int Add(int a, int b) { return a + b; } public void MethodThatTakesAdd(AddDelegate addFunction) {

how to use cdecl callback with pinvoke

可紊 提交于 2019-12-20 20:00:38
问题 I have a c library that has cdecl callbacks. How can I use these from c#. Everything seems to say that they must be stdcall callbacks to be clear: delegate int del(); [dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)] public static extern int funcwithcallback(del foo); where del must be called cdecl-wise 回答1: Take a look at this. The functionality has been around since 1.1 so it should cover whatever .NET version you are using. You just have to specify the CallingConvention.

int vs IntPtr when you have a handle?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 19:40:29
问题 First a background question: In general, what is the difference between int and IntPtr ? My guess is that it is an actual object rather than a value like an int or byte is. Assuming that is true: So they are not the same. Yet I see handles represented as both. IntPtr: Control.Handle int (or uint): A PInvoke can be setup to return an int and it works just fine: [DllImport("coredll.dll", SetLastError = true)] public static extern int GetForegroundWindow(); private string GetActiveWindow() {

Marshalling a char** in C#

醉酒当歌 提交于 2019-12-20 19:39:46
问题 I am interfacing with code that takes a char** (that is, a pointer to a string): int DoSomething(Whatever* handle, char** error); Basically, it takes a handle to its state, and if something goes wrong, it returns an error code and optionally an error message (the memory is allocated externally and freed with a second function. That part I've figued out :) ). I, however, am unsure how to handle in in C#. What I have currently: [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl

Wrap native DLL for C#

拈花ヽ惹草 提交于 2019-12-20 12:42:07
问题 I wrote a C++ DLL and now I need to call a native function from a managed app. The exported native function appears like this: extern "C" __declspec(dllexport) bool NativeMethod(char *param1, char *param2, char *result); So, from C# I'll call that function passing 2 input params, 1 output param and obviously I'll read the return bool value. I tried to wrap all this in many ways, but always I get a PInvokeStackImbalance exception. The only way I know to call native function is by applying

How to marshal an unknown length C++ string to C# using its pointer?

痞子三分冷 提交于 2019-12-20 06:27:45
问题 I'm trying to marshal a dynamically allocated char array in a struct to C#. The struct has a pointer to the array. The problem is the char array contains multiple null terminated strings and the last string is terminated by two consecutive null chars. If I try to marshal it as LPStr I will get only de first string in the "list". I tried using UnmanagedMemoryStream but it requires to know the length of the array. Is there a way to read the bytes as a stream without knowing the length of the

Use a variable directly(data return are same in an array) or with pointer?

ε祈祈猫儿з 提交于 2019-12-20 05:41:25
问题 I had two flock of bird sensor and i have refer to example code #8 in manual, the c++ code is working for me. But i only get one sensor data from the c sharp project. The problem is that in the C# example, bird_data[1] and bird_data[2] appear to have same position data. In the C++ example both bird_data[1] and bird_data[2] have correct data.I get the same position output data from below code. text output in the LBird1X is same LBird2X,LBird1Y is same LBird2Y and LBird1Z is same LBird2Z. Could