pinvoke

p/invoke calling C dll from c#

妖精的绣舞 提交于 2019-12-23 05:35:09
问题 this is my C code extern "C" { __declspec(dllexport) void DisplayHelloFromDLL(string a) { printf ("%s\n",a) } } this is my C# code class HelloWorld { [DllImport("TestLib.dll")] public static extern void DisplayHelloFromDLL(string a); static void Main () { string a = "Hello"; DisplayHelloFromDLL(a); } } It built successfully but crash like this: SO,how to use P/invoke to call my own C dll from C#? Please help,thanx in advance. 回答1: First of all your code is C++ rather than C. Your function

Japanese to Romaji with Kakasi

*爱你&永不变心* 提交于 2019-12-23 04:01:49
问题 I want to transliterate Japanese to Romaji with Kakasi tool, using C#. For this, I created a wrapper: [DllImport("kakasi.dll")] static extern int kakasi_getopt_argv(int size, IntPtr param); [DllImport("kakasi.dll")] static extern IntPtr kakasi_do([MarshalAs(UnmanagedType.LPStr)]string str); public static void SetParams(string [] paramz) { kakasi_getopt_argv(paramz.Length, StringToIntPtr(paramz)); } public static string DoKakasi(string japanese) { return Marshal.PtrToStringAuto(kakasi_do

WinDivert in C#

三世轮回 提交于 2019-12-23 03:31:17
问题 I want to call 5 WinDivert functions in a C# code, first reflex : PInvoke here are the signatures : internal enum WINDIVERT_LAYER { WINDIVERT_LAYER_NETWORK = 0, /* Network layer. */ WINDIVERT_LAYER_NETWORK_FORWARD = 1 /* Network layer (forwarded packets) */ } internal const UInt64 WINDIVERT_FLAG_SNIFF = 1; /* * typedef struct { UINT32 IfIdx; UINT32 SubIfIdx; UINT8 Direction; } WINDIVERT_ADDRESS, *PWINDIVERT_ADDRESS; * */ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] internal

Method's type signature is not PInvoke compatible

若如初见. 提交于 2019-12-23 03:17:16
问题 I am trying to use a function from a c dll, in a c# application, I am facing this error each time i try to run the application and call the function in question. At first i thought maybe this is because of the wrong signature i am using, but i tried to make it as simple as possible yet no luck.! To cut a long story short: This is my actual source code for c dll : #include <stdio.h> #include <string.h> extern "C" { struct teststruct { char acharacter; int anumber; char* astring; char anarray

NetUserGetInfo/NetLocalGroupGetInfo returning error 1722

对着背影说爱祢 提交于 2019-12-23 03:15:26
问题 I have the following code (VB.NET) which is designed to determine if a given account name refers to a local group or a user account. This will only be called for accounts/groups on a machine, not a domain. Module netapi Private Declare Function NetUserGetInfo Lib "Netapi32.dll" ( _ ByVal ServerName As String, _ ByVal UserName As String, _ ByVal level As Integer, _ ByRef BufPtr As IntPtr) As Integer Private Declare Function NetLocalGroupGetInfo Lib "Netapi32.dll" ( _ ByVal ServerName As String

NetUserGetInfo/NetLocalGroupGetInfo returning error 1722

邮差的信 提交于 2019-12-23 03:15:03
问题 I have the following code (VB.NET) which is designed to determine if a given account name refers to a local group or a user account. This will only be called for accounts/groups on a machine, not a domain. Module netapi Private Declare Function NetUserGetInfo Lib "Netapi32.dll" ( _ ByVal ServerName As String, _ ByVal UserName As String, _ ByVal level As Integer, _ ByRef BufPtr As IntPtr) As Integer Private Declare Function NetLocalGroupGetInfo Lib "Netapi32.dll" ( _ ByVal ServerName As String

Getfield.SetValue doesn't work

自作多情 提交于 2019-12-23 02:54:21
问题 in my project i'm currently working on i stumbled upon this problem: I want to create a Instance of the Class "ApiID". I got the Code from Reflector, as you can see the .dll (not my Project) imports is from ummanaged code, which i don't have access to. [StructLayout(LayoutKind.Sequential, Size=0x10), CLSCompliant(false), NativeCppClass, DebugInfoInPDB, MiscellaneousBits(0x40)] public struct ApiId { private long <alignment member>; public static unsafe void <MarshalCopy>(ApiId* idPtr1, ApiId*

How do I call this Delphi method in C# using Dllimport?

送分小仙女□ 提交于 2019-12-23 02:25:09
问题 New Programmer in need of Help! The Delphi code that is compiled into the DLL function SetCurrentSerial(Size : Integer; Msg : Pointer) : Integer stdcall; var TempByte : PByte; TempStr : string; i: Integer; begin Result := 0; TempByte := Msg; TempStr := ''; for i := 0 to Size - 1 do begin TempStr := TempStr + ' '; end; for i := 0 to Size - 1 do begin TempStr[i+1] := Chr(TempByte^); Inc(TempByte); end; if not DLLClass.SelectDeviceSerial(TempStr) then begin Result := -1; end; end; The C# Code /

Return C++ arrays to C#

社会主义新天地 提交于 2019-12-23 02:09:08
问题 I am trying a small interop application, in which I am invoking some C++ methods from C#. I have a very basic example woking for invoking a method and returning an integer, which works just fine. InteropApp.h #ifdef DLLFUNCTIONEXPOSETEST_EXPORTS #define DLLFUNCTIONEXPOSETEST_API __declspec(dllexport) #else #define DLLFUNCTIONEXPOSETEST_API __declspec(dllimport) #endif extern "C" DLLFUNCTIONEXPOSETEST_API int fnSumofTwoDigits(int a, int b); InteropApp.cpp #include "stdafx.h" #include

waveout sometimes memory issue

送分小仙女□ 提交于 2019-12-23 01:36:14
问题 This code works to output a piano tone for 2 seconds using winmm.dll via platform invocation services, it seems to work fine on XP but waveoutopen fails in windows 7 rc updated based on feedback from John Knoeller Imports System.Runtime.InteropServices Public Class AudioStream <StructLayout(LayoutKind.Sequential)> _ Private Structure WAVEHDR Public lpData As Integer Public dwBufferLength As Integer Public dwBytesRecorded As Integer Public dwUser As Integer Public dwFlags As Integer Public