pinvoke

Using PInvoke vs .NET provided functions

From version to version of .NET the more function that's equal to P/Invoke is added to .NET Now there are 2 questions in my mind. 1) Which one is prefer over the other in term of speed, normally I use .Net function but in tight loop I don't really know which one is going to be faster. 2) Is there any website that provide the list of c...

PInvoke Unbalances the stack

Good afternoon, I have been working on a dll that can use CORBA to communicate to an application that is network aware. The code works fine if I run it as a C++ console application. However, I have gotten stuck on exporting the methods as a dll. The methods seems to export fine, and if I call a method with no parameters then it works...

Find window with specific text for a Process

Hello, I'm trying to find if a window with specific has been open by a Process. That process spawns multiple windows, and I need to check them all. I have no trouble finding the process, with foreach (Process p in Process.GetProcesses()) { if (p.MainModule.FileName.ToLower().EndsWith("foo.exe")) FindChildWindowWithText(p); //do ...

c# pinvoke marshall struct

Hi, I have an unmanaged struct I'd like to marshal to c# that looks basically like this: struct DateTimeStruct{ double datetimestamp; }; struct MyStruct{ char firstname[40]; char lastname[40]; DateTimeStruct bday; unsigned integer bool1; int val1; }; What is the the correct c# declaration? ...

Retrieving virtual disk file name from disk number

When I list virtual disks within diskpart: DISKPART> list vdisk VDisk ### Disk ### State Type File --------- -------- -------------------- --------- ---- VDisk 0 Disk 2 Attached not open Fixed C:\Disk.vhd Interesting part for me here is file name. I tried to find equivalent of functio...

Setting system date and time from the application

I have a application that is developed using .Net Compact frame work 3.5, i need to synchronize the device date and time with the central server on every web service call. Thanks. ...

p/invoke GetSystemTime() method is giving wrong output

I used p/invoke GetSystemTime() method in my application to get the current system date time but it is giving wrong values any solution for this.. ...

How to capture the event if a new process (application!) is started?

I would like to have some eventhandler which raise if a new application is started. I've heard that this is possible by using a hook but the only examples I can find are based on mouse/keyboard events. What is an example link of how I can create such a hook in C#? Oh and btw: Nope, I don't want to use WMI which could be a solution as w...

Does the managed main UI thread stay on the same (unmanaged) Operation System thread?

I am creating a managed WPF UI front-end to a legacy Win32-application. The WPF front-end is the executable; as part of its startup routines I start the legacy app as a DLL in a second thread. Any UI-operation (including CreateWindowsEx, etc.) by the legacy app is invoked back on the main UI-thread. As part of the shutdown process of th...

Why Microsoft not provide for C# a static Win32 class with the most native functions and structures inside like windows.h?

Everybody who used P/Invoke of Windows API knows a long list of declarations of static functions with attributes like [DllImport ("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] The declaration of structures copied from Windows headers like WinNT.h or from web sites like www.pinvoke.net take also a lot of place in our p...

PInvoke or using /clr:pure to compile

I have a set of numerical libraries in C++ and I want to call them interactively in a interpretive language like F# or IronPython. So I have two choices now: Compile the library in native DLL and use PInvoke to call functions in it. Compile the C++ library to .Net dll using visual c++ (/clr:pure compile option). The advantage of 1...

Bring up the printer settings dialog and have the changes saved

This is the same as this question, with one remark: We manage to get the printerSettings modified, but how can we save them back as the default printer settings? (the original question does not post/answer this) The code I'm using right now: <DllImport("winspool.Drv", EntryPoint:="DocumentPropertiesW", SetLastError:=True, _ ExactSp...

Reverse PInvoke and create a full unmanaged C# program

I know this is a strange question but the idea is simple: I prefer C# syntax rather than C++: -Setters and getters directly inside a property -interfaces -foreach statement -possibility to declare an implicit cast operator other small things... What I really don't know is if is possible to import a c++ dll (expecially std libraries) in...

Set DllImport attribute dynamically

I am making use of an external unmanaged dll using PInvoke and the DllImport attribute. eg. [DllImport("mcs_apiD.dll", CharSet = CharSet.Auto)] private static extern byte start_api(byte pid, byte stat, byte dbg, byte ka); I am wondering if it is possible to alter the dll file details (mcs_apiD.dll in this example) dynmically in some...

P-invoke call fails if too much memory is assigned beforehand

I've got a p-invoke call to an unmanaged DLL that was failing in my WPF app but not in a simple, starter WPF app. I tried to figure out what the problem was but eventually came to the conclusion that if I assign too much memory before making the call, the call fails. I had two separate blocks of code, both of which would succeed on their...

PostMessage does not seem to be working.

I am trying to use PostMessage to send a tab key. Here is my code: // This class allows us to send a tab key when the the enter key // is pressed for the mooseworks mask control. public class MaskKeyControl : MaskedEdit { // [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)] // static extern IntPtr SendMessag...

How do you place an Excel Sheet/Workbook onto a C# .NET Winform?

I am trying to create a stand alone application in Visual Studio 2008 C# .Net that will house Excel Workbooks (2007). I am using Office.Interop in order to create the Excel application and I open the workbooks via Workbooks.Open(...). The Interop does not provide any functionality to "move" the workbooks onto a form so I turned to P/I...

Using Window Handle to disable Mouse clicks and Keyboard Inputs using P/Invoke

I need to disable the Mouse Clicks, Mouse movement and Keyboard Inputs for a specific windows for a Kiosk application. Is it feasible using .NET ? I have removed the menu bar and title bar of a specific window, will that be a starting point to achieve the above requirement ? The code for removing the menu bar and title bar using window...

P/Invoke a Function Passed a StringBuilder

in a C# file i have a class Archiver { [DllImport("Archiver.dll")] public static extern void archive(string data, StringBuilder response); } string data is an input, and StringBuilder response is where the function writes something the archive function prototype (written in C) looks like this: void archive(char * dataChr, ch...

Win32 API P-Invoke to bring a disk online, offline, and set unique ID

I am currently using Diskpart to accomplish these functions, but i would like to be able to use P-Invoke and not have to shell out to an external process in my C# app. The example Diskpart scripts are: //Online a disk Select disk 7 disk online // Reset GPT Identifier select disk 7 UNIQUEID DISK ID=baf784e7-6bbd-4cfb-aaac-e86c96e166ee...