pinvoke

PInvoking with StringBuilder in a Unit Test

我的未来我决定 提交于 2019-12-25 08:59:12
问题 I have a C DLL I am PInvoking. The main goal is to get back a GUID string of 39 characters, such as abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd . I first call one method to get the size of this string, which I expect to be 39 characters, and then I call another function passing it a StringBuilder with a capacity of 39: [DllImport("test.dll")] public static extern int get_size(); [DllImport("test.dll")] public static extern void get_string(StringBuilder result); My code looks something like this:

Using Winamp's in_midi.dll in .NET

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 08:56:41
问题 I am trying to load a Winamp input plugin and work with it in C#. According to the Winamp SDK, this is the proper way to load a plugin: in_mp3_lib = LoadLibraryW(path); if (in_mp3_lib) { PluginGetter pluginGetter = (PluginGetter)GetProcAddress(in_mp3_lib, "winampGetInModule2"); if (pluginGetter) { in_mp3 = pluginGetter(); } } So I've created a similar code in C#, after learning how to add a msvcr90.dll dependency to the manifest: [DllImport("kernel32", SetLastError=true, CharSet=CharSet

Pinvoke upon return of int, API

笑着哭i 提交于 2019-12-25 08:09:06
问题 I am currently running into an issue of calling an Win32 DLL[Native] from a C# windows application. I have come so far. C++ source: extern "C" __declspec(dllexport) int PlaceSound(__in DWORD frequence, __in DWORD duration) { Beep(frequence, duration); return 0; } C# source: [DllImport("SampleLib.dll")] public extern static int PlaceSound(int Freq, int Dura); public form1 { InitializeComponements; PlaceSound(150, 500); } Upon debugging, I recieve the sound, however, when the library returns

Cannot p/invoke C method from C# due to marshalling issues

早过忘川 提交于 2019-12-25 07:43:03
问题 I'm trying to invoke the I2CTransfer function below, and immediately getting a System.NotSupportedException . I suspect my marshalling is wrong, but cannot work out the problem. Here are the C structures: BOOL I2CTransfer(HANDLE hDev, PI2C_TRANSFER_BLOCK pI2CTransferBlock); typedef struct { I2C_PACKET *pI2CPackets; INT32 iNumPackets; } I2C_TRANSFER_BLOCK, *PI2C_TRANSFER_BLOCK; typedef struct { BYTE byAddr; BYTE byRW; PBYTE pbyBuf; WORD wLen; LPINT lpiResult; } I2C_PACKET, *PI2C_PACKET; And

CRITICAL_SECTION in c#

落爺英雄遲暮 提交于 2019-12-25 06:28:16
问题 All. I need to use winapi critical section in c# code. First of all, I import functions: [StructLayout(LayoutKind.Sequential)] public struct CRITICAL_SECTION { public int dummy; } // INIT CRITICAL SECTION [DllImport("kernel32.dll")] static extern bool InitializeCriticalSectionAndSpinCount(ref CRITICAL_SECTION lpCriticalSection, uint dwSpinCount); // DELETE CRITICAL SECTION [DllImport("kernel32.dll")] static extern void DeleteCriticalSection(ref CRITICAL_SECTION lpCriticalSection); // ENTER

C# P/Invoke and array of structs containing byte arrays

泄露秘密 提交于 2019-12-25 04:46:35
问题 I need to invoke a native DLL from C# code. As I am not very familiar with C/C++, I can't figure out how a structure defined in C should be declared in C# so it can be invoked. The problem is that two parameters seems to be an array of structs, which I don't know how to declare this in C# (see last code block): c++ header file: typedef enum { OK = 0, //others } RES typedef struct { unsigned char* pData; unsigned int length; } Buffer; RES SendReceive(uint32 deviceIndex Buffer* pReq, Buffer*

Passing an array of strings from C# to C++ DLL function and fill it in the DLL and get back

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:16:20
问题 I am writing a C# application that passes an empty string array of size 30 to a C++ DLL. This string array needs to be filled in the DLL and given back to the C# application. I my code I observe memory corruption at the end of function call from DLL. My C++ DLL code is as follows: SAMPLEDLL_API BOOL InitExecution (wchar_t **paszStrings, int count) { for (int i = 0 ; i < count; i++) { mbstowcs(*(paszStrings + i), "Good",4); //*(paszStrings + i) = "Good"; } return TRUE; } My C# code is string[]

PInvoke FbwfFindFirst - FbwfCacheDetail problems

寵の児 提交于 2019-12-25 03:11:33
问题 I'm trying to create a PInvoke for FbwfFindFirst and am struggling with the struct FbwfCacheDetail. In short, I'm not sure how to marshal WCHAR fileName[1]; seeing as it's a variable length array and a non-null terminated. Any help would be welcomed 回答1: Since the whole structure is of variable size, one way to do this is like this (I can't test it because I don't have this dll on my system): string volume = ""; int size = 0; // ask for whole structure size FbwfFindFirst(volume, IntPtr.Zero,

How can I properly implement inetcpl.cpl as an external dll?

纵然是瞬间 提交于 2019-12-25 03:09:05
问题 I have the following 2 sets of code, both of which produce the same results: using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ResetIE { class Program { [DllImport("InetCpl.cpl", SetLastError=true, CharSet=CharSet.Unicode)] public static extern long ClearMyTracksByProcessW(IntPtr hwnd, IntPtr hinst, ref TargetHistory lpszCmdLine, FormWindowState nCmdShow); static void Main(string[] args) {

Disable Windows Visual Effects through SystemParametersInfo

戏子无情 提交于 2019-12-24 22:25:13
问题 I am trying to disable all visual effects programmatically in a Windows Forms App. Other than an extense list of registry values to be changed, i found this option, but I cant seem to get it to work. Searched in pinvoke.net and on MSDN for a more specific answer, but I could not find one. So, heres the data I have: Info about SPI_SETUIEFFECTS SPI_SETUIEFFECTS 0x103F Enables or disables UI effects. Set the pvParam parameter to TRUE to enable all UI effects or FALSE to disable all UI effects. I