标签: pinvoke

P/Invoke SetupDiGetDeviceInterfaceDetail 返回 1784

我目前正在尝试 P/Invoke 到 HID(在 Unity 中(因此使用 .Net 2.0))。但是,当调用SetupDiGetDeviceInterfaceDetail时,它返回错误代码1784。

注意:这是第二次调用,因为第一次调用应该(并且确实)返回错误 122,并设置所需的缓冲区大小。

..Replaced With Full Source Below..
Run Code Online (Sandbox Code Playgroud)

使用的结构:

..Replaced With Full Source Below..
Run Code Online (Sandbox Code Playgroud)

目前未使用 DETAIL_DATA,因为我发现我应该使用 IntPtr 来代替。

如何摆脱错误 1784 并获取/迭代我的设备路径?另外,我读到我应该使用“null”,而不是 dData,但这也会出错(无法将 null 转换为 DEVICE_INTERFACE_DATA)

编辑:完整来源

调试输入.cs:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using Assets;

public class DebugInput : MonoBehaviour {
    static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () { …
Run Code Online (Sandbox Code Playgroud)

.net c# pinvoke unity-game-engine

0
推荐指数
1
解决办法
2265
查看次数

从 C++ DLL 调用时,带有参数的 C# 委托回调会导致 AccessViolation

我有一个非托管 C++ DLL 和一个使用 P/Invoke 进行通信的 .net 应用程序。我需要从 C++ DLL 更新 .net 应用程序中的字典。

我尝试通过以下方式做到这一点:

public delegate void MyDelegate(string address, string username);

[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SaveMyDelegate(MyDelegate callback);

private static SortedDictionary<string, string> dict = new SortedDictionary<string, string>();

public void save_delegate() {

    SaveMyDelegate(delegate(string address, string username) {
        if (username != "") {
            dict.Add(address, username);
        }
     });
 }
Run Code Online (Sandbox Code Playgroud)

在 C++ 方面我有:

typedef void (*MANAGED_CALLBACK)(string user_address, string username);

    extern "C" __declspec(dllexport) void SaveMyDelegate(MANAGED_CALLBACK callback);

    MANAGED_CALLBACK my_callback;
    void SaveMyDelegate(MANAGED_CALLBACK callback) { …
Run Code Online (Sandbox Code Playgroud)

.net c# c++ pinvoke unmanaged

0
推荐指数
1
解决办法
1104
查看次数

vb.net中的LockWindowUpdate而不使用dll?

我是从vb到vb.net的API转换的初学者。在vb6中,它们使用user32 DLL。在Vb.Net中,我需要调用LockWindowUpdate Function()而不使用“ User32.dll”函数。

在不使用vb​​.net中任何API调用的情况下,有什么方法可以锁定窗口状态?

如果等效的话,请让我告诉我这些代码。该代码应在.net框架中使用。

VB

Private Declare Function LockWindowUpdate Lib "user32"
  (ByVal hWnd As Long) As Long

LockWindowUpdate Form1.hWnd
Run Code Online (Sandbox Code Playgroud)

c# vb.net pinvoke vb6-migration

-1
推荐指数
1
解决办法
1461
查看次数

我应该如何导入Powerflex函数以用于C#?

我被赋予了在Visual Studio 2010 C#中使用pfxlibn.dll的任务,我从中可以看出它是一个Powerflex库.http://pfxcorp.com/clib-fns.htm

我尝试了一些导入PclGetField函数的变体,例如:

[DllImport("pfxlibn.dll", SetLastError = true)]
public static extern void PclGetField(
    Int32 iHandle, 
    Int32 iFieldNum, 
    [MarshalAs(UnmanagedType.LPStr)] string Date
    ); 

[DllImport("pfxlibn.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi,
    EntryPoint = "PclGetField")]
public static extern int PclGetField(Int32 iHandle, Int32 iFieldNum, string Data)

[DllImport("pfxlibn.dll", EntryPoint = "PclGetField",
    ExactSpelling = true,
    CharSet = CharSet.Ansi,
    CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 PclGetField(Int32 iHandle, Int32 iFieldNum,[MarshalAs(UnmanagedType.VBByRefStr)] ref string input);
Run Code Online (Sandbox Code Playgroud)

到目前为止,上述都没有奏效.或以上的变化.

在Delphi中,代码看起来像

pTmpBuf           : PChar;

iLastErr := PclGetField(fHandle,iField,pTmpBuf);
Run Code Online (Sandbox Code Playgroud)

c# delphi pinvoke

-1
推荐指数
1
解决办法
288
查看次数

我怎样才能将C#header转换为delphi

我怎么能用Delphi代码写这个呢?

[DllImport("FT_ND_API.dll")]
public static extern uint epas_CreateContext(out IntPtr hContextHandle, 
    uint ulFlags, uint ulApiVersion);

[DllImport("FT_ND_API.dll")]
public static extern uint epas_OpenDevice(IntPtr hContextHandle, 
    uint ulQueryType, IntPtr pQueryData);

[DllImport("FT_ND_API.dll")]
public static extern uint epas_GetProperty(IntPtr hContextHandle, uint ulFlags, 
    IntPtr pRefData, byte[] pPropData, uint ulPropSize);

[DllImport("FT_ND_API.dll")]
public static extern uint epas_CreateDir(IntPtr hContextHandle, uint ulFlags, 
    String pucName, String pucGuid, ref WDirInfo pDirInfo, uint ulSizeOfDirInfo);
Run Code Online (Sandbox Code Playgroud)

c# delphi pinvoke

-1
推荐指数
1
解决办法
406
查看次数

从C#.Net 2010开始推出VB6功能

我想PInvoke从C#用VB6编写的函数.我们可以在VB6中调用这个函数,如下所示:

Declare Function ApplyNNet Lib " location of the DLL file" (MyNNetType As String, MyAddress As String, MyInput() As Double) As Variant
Run Code Online (Sandbox Code Playgroud)

我在C#中的代码是:

[DllImport("NNetApply.dll", EntryPoint = "ApplyNNet", CallingConvention = CallingConvention.StdCall)] 
public static extern IntPtr ApplyNNet(string type, string add, double[,] data);
//run ANN with data
string address = @"C:\Users\PNGE-User\Desktop\Faegheh\Project\Neural Network For Pressure Vs q,x,y\P ve x,y,q\P ve x,y,q";
        double[,] P = new double[no_data,2];
        var P_ = ApplyNNet("Back Prop", address, data);
Run Code Online (Sandbox Code Playgroud)

当我调试我的代码时,出现错误:

尝试读取或写入受保护的内存.这通常表明其他内存已损坏.

c# vb6 pinvoke

-1
推荐指数
1
解决办法
631
查看次数

C到C#数组编组中的矛盾行为

我有两个几乎相同代码的例子.我可以从C#中检索一个数据,但我没有为另一个人提供正确的数据.他们来了:

效果很好

C++部分:

__declspec(dllexport) void** enumerateDevices(int *dIsize){
    Array<DeviceInfo> dIArray;
    Framewoek::enumerateDevices(&dIArray);
    *dIsize = dIArray.getSize();
    DeviceInfo dP[255];
    for (int i = 0; i < dIArray.getSize(); i++)
        dP[i] = dIArray[i];
    void* p = dP;
    return &p;
}
Run Code Online (Sandbox Code Playgroud)

C#部分:

    [DllImport("Wrapper.dll")]
    static extern IntPtr enumerateDevices(out int devicesSize);
    public static DeviceInfo[] EnumerateDevices()
    {
        int devicesSize;
        IntPtr arrayPointer = enumerateDevices(out devicesSize);
        IntPtr[] array = new IntPtr[devicesSize];
        Marshal.Copy(arrayPointer, array, 0, devicesSize);
        DeviceInfo[] arrayObjects = new DeviceInfo[devicesSize];
        for (int i = 0; i < devicesSize; i++)
            arrayObjects[i] = new …
Run Code Online (Sandbox Code Playgroud)

c# c++ pinvoke marshalling

-1
推荐指数
1
解决办法
290
查看次数

delphi记录到C#

需要帮助将delphi记录转换为C#struct并将此结构传递给C dll:

type Transaction = record
  pos_id: array[0..9] of char;
  ev_type: array[0..4] of char;
  amount: array[0..14] of char;
  chip_reader_used: boolean;
  internal_magstripereader_used: boolean;
  track2data: array[0..99] of char;
  card_usage_mode: array[0..4] of char;
  receipt_directory: array[0..254] of char;
  additional_info: array[0..999] of char;
  event_number: array[0..9] of char;
  original_filingcode: array[0..19] of char;
  transaction_reasoncode: array[0..9] of char;
end;
Run Code Online (Sandbox Code Playgroud)

试过以下但没有工作:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public unsafe struct Transaction {
        public fixed byte pos_id[10];
        public fixed byte ev_type[5];
        public fixed byte amount[15];
        public bool       chip_reader_used;
        public …
Run Code Online (Sandbox Code Playgroud)

c# delphi pinvoke

-1
推荐指数
1
解决办法
950
查看次数

GetActiveWindow返回0

        IntPtr win = GetActiveWindow();
        RECT dimensions;
        GetWindowRect(win, out dimensions);
Run Code Online (Sandbox Code Playgroud)

上面的代码似乎返回一个矩形,其尺寸为0.Right和dimensions.Left值.我对winapi的引用如下.

    [DllImport("user32.dll", SetLastError=true)]
    static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

    enum GetWindow_Cmd : uint
    {
        GW_HWNDFIRST = 0,
        GW_HWNDLAST = 1,
        GW_HWNDNEXT = 2,
        GW_HWNDPREV = 3,
        GW_OWNER = 4,
        GW_CHILD = 5,
        GW_ENABLEDPOPUP = 6
    }
    [DllImport("user32.dll", SetLastError=true))]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;        // x position of upper-left corner …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke winapi

-1
推荐指数
1
解决办法
2645
查看次数

必须做FreeLibrary 2次,虽然我只做了一次LoadLibrary.此外,卸载DLL后,尝试再次加载时,会发生错误

有以下C#代码用于加载和卸载C++ DLL.

我只加载DLL一次,但代码必须卸载DLL 2次.在卸载DLL之后,当我再次加载它,并且我调用DLL的导出函数时,我收到以下错误消息:

尝试读取或写入受保护的内存.这通常表明其他内存已损坏.

DLL依赖于其他DLL.

    /// //////////////handle of FDD DLL:
    System.IntPtr SystemIntPtr_handle_of_DLL=System.IntPtr.Zero;

    private void button4_Click(object sender, EventArgs e)
    {
        try
        {
            string string_Dependency_path = ".\\DLL_Dependencies\\";
            Call_DLL.SetDllDirectory(string_Dependency_path);
            SystemIntPtr_handle_of_DLL = Call_DLL.LoadLibrary("DLL.dll");
            if (SystemIntPtr_handle_of_DLL == System.IntPtr.Zero) { throw new Exception("DLL did not load"); }

        }
        catch (Exception Exception_Object) { MessageBox.Show(Exception_Object.Message); }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        try
        {
            int int_FreeLibrary_counter = 0;
            while (Call_DLL.FreeLibrary(SystemIntPtr_handle_of_DLL))
            {
                int_FreeLibrary_counter++;
            }
            MessageBox.Show("DLL unloaded. You will have to load it again. (Unloaded" + int_FreeLibrary_counter …
Run Code Online (Sandbox Code Playgroud)

c# c++ dll pinvoke loadlibrary

-1
推荐指数
1
解决办法
1400
查看次数