dllimport

Exporting global variables from DLL

 ̄綄美尐妖づ 提交于 2019-12-04 16:42:00
问题 I'm trying to export a global variable from a DLL. Foo.h class Foo { public: Foo() {} }; #ifdef PROJECT_EXPORTS #define API __declspec(dllexport) #else #define API __declspec(dllimport) #endif API const Foo foo; Foo.cpp #include "Foo.h" const Foo foo; When I compile the above code, Visual Studio complains: foo.cpp(3) : error C2370: 'foo' : redefinition; different storage class 1> foo.h(14) : see declaration of 'foo' If I use: external const Foo foo; in Foo.h the compiler is happy but then the

Passing method pointer from C# to Delphi DLL

雨燕双飞 提交于 2019-12-04 13:52:59
I've had some problems passing string as PChar to Delphi built DLL, and resolved it thanks to Jens Mühlenhoff. Now I have another issue - I've made successful callback of c# method when passed to DLL if the Delphi declaration is a regular type procedure, but if Delphi declaration is a method type procedure I get "Attempted to read or write protected memory" error. I tried searching... Here is Delphi declaration TCallBack = procedure ( s : String) of object;stdcall; Here is C# code [DllImport( "DLLTest.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint =

P/Invoke with Shell32, bypass Interop.Shell32.dll generation

雨燕双飞 提交于 2019-12-04 13:00:34
So I'm using the following code to dump every extended file attribute to the debug console... ShellClass shellClass = new ShellClass(); Folder dir = shellClass.NameSpace(Path.GetDirectoryName(sFilePath)); FolderItem item = dir.ParseName(Path.GetFileName(sFilePath)); for (int i = 0; i < 267; i++) { System.Debug.WriteLine(dir.GetDetailsOf(item, i)); } As Shell32 is a non-managed library, I can't embed the DLL resource in the program directly, and it has to generate an interop helper DLL. Anyone know of the DLLImport command lines for these functions in Shell32 to get around the whole generation

C# formatting external Dll function parameters

为君一笑 提交于 2019-12-04 11:45:07
I have yet to find a good reference on this topic. For this example we will take some C++ code that I'm trying to port over to C#. In C++ land we have the following prototype for an external library function: extern "C" DWORD EXPORT FILES_GetMemoryMapping( PSTR pPathFile, PWORD Size, PSTR MapName, PWORD PacketSize, PMAPPING pMapping, PBYTE PagesPerSector); and in it is used like such: FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector); Now I'm trying to port the first line over to C# and here is where I'm presented with no clear path.

Is there any way to debug what is going on after my .NET code calls a function in an unmanaged dll via dllimport?

蓝咒 提交于 2019-12-04 10:55:56
Is there any way to debug what is going on after my .NET code calls a function in an unmanaged dll via dllimport? I expose a dll function inside an unmanaged via dllimport. When I call a function as I step through the code, something happens and it never returns. Is there anything I can do, maybe with debug view or anything to get any information? Or am I SOL? Well, for starters, make sure you have native code debugging enabled in the Debug tab of your project's properties. If you have the source code for the DLL you are calling into, then you should be able to set it up so that you can step

Add a UAC shield to a button and retain its background image?

。_饼干妹妹 提交于 2019-12-04 09:29:40
问题 Using C# and .Net 4.0 in a winforms application: Is it possible to add the UAC shield to a button and retain the buttons background image? How? This is what I'm using at the moment, but it removes the image... [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); public static void UACToButton(Button button, bool add) { const Int32 BCM_SETSHIELD = 0x160C; if (add) { // The button must have the flat style button.FlatStyle = FlatStyle

FindWindowEx from user32.dll is returning a handle of Zero and error code of 127 using dllimport

眉间皱痕 提交于 2019-12-04 09:19:11
问题 I need to handle another windows application programatically, searching google I found a sample which handles windows calculator using DLLImport Attribute and importing the user32.dll functions into managed ones in C#. The application is running, I am getting the handle for the main window i.e. Calculator itself, but the afterwards code is not working. The FindWindowEx method is not returning the handles of the children of the Calculator like buttons and textbox. I have tried using the

DLLImport Int** - How to do this if it can be done

安稳与你 提交于 2019-12-04 08:20:55
I am trying to use a third party DLL that wants an int** as one of the parameters to the method. It describes the parameter as the address of the pointer that will point to the memory allocation. Sorry for any confusion. The parameter is two-way I think. The DLL is for talking to an FPGA board and the method is setting up DMA transfer between the host PC and the PCI board. Use a by-ref System.IntPtr . [DllImport("thirdparty.dll")] static extern long ThirdPartyFunction(ref IntPtr arg); long f(int[] array) { long retval = 0; int size = Marshal.SizeOf(typeof(int)); var ptr = IntPtr.Zero; try {

Use GetForegroundWindow result in an if statement to check user's current window

扶醉桌前 提交于 2019-12-04 07:23:56
I need to check what window the user currently has selected, and do stuff if they have a specific program selected. I haven't used the GetForegroundWindow function before, and can't find any information on how to use it in this manner. I simply need an if comparing the current window to see if its a specific program. However the GetForegroundWindow function doesn't give back a string or int it seems. So mainly I don't know how to find out the value of the program window I want to compare it to. I currently have the code to get the current window: [DllImport("user32.dll")] private static extern

How do I properly import functions from a C++ DLL into my C# Project

半腔热情 提交于 2019-12-04 07:15:36
问题 I could use help with how to properly import several functions from a C++ DLL into my C# application. Here are a couple examples from the C++ side that show what I am trying to do in C#. I do not think I am properly marshaling either/or some of the return types and some of the parameters (especially pointers/ref/out). C++ Header File Declarations: unsigned long __stdcall mfcsez_initialisation(unsigned short serial); unsigned char __stdcall mfcs_get_serial(unsigned long int handle, unsigned