标签: pinvoke

如何判断.NET编译代码是否使用p/invoke?

判断.NET编译代码是否使用p/invoke的最简单方法是什么?

.net pinvoke

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

如何从javascript调用user32.dll方法

我在浏览器上运行了一个javascript.是否可以在user32.dll中调用函数/方法.

通过使用pInvoke调用可以从C#实现.我如何在JavaScript中执行相同的操作?

谢谢,

Datte

javascript pinvoke user32

1
推荐指数
2
解决办法
1万
查看次数

编组包含字符串的结构

我基本上想要从c#中的用户获取int name和string age并将其发送到用c编写的dll方法,其中包含int和char [50]参数并返回字符串.i创建以下场景但我失败了,任何正文都有代码

我有一个在c开发的dll,它有一个结构

struct Argument 
{
int age;
char name[50];
} ;
Run Code Online (Sandbox Code Playgroud)

和方法

extern "C"
{
    __declspec(dllexport) Argument FillData(Argument data)
 {
        Argument mydata;

        mydata.age=data.age;
        for(int i=0;i<=sizeof(data);i++)
        {
            mydata.name[i]=data.name[i];

        }
        return mydata;

 }
Run Code Online (Sandbox Code Playgroud)

我在Cs_dll.cs中的c#中声明它

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
    public struct Argument
    {
        public int age;
        [MarshalAs(UnmanagedType.TBStr)]
        //public char name;
       public char[] name;

    };
  public  class Cs_Dll
    {
      [DllImport("TestLib.dll")]
            public static extern Argument FillData (Argument data);


    }
Run Code Online (Sandbox Code Playgroud)

现在再按一个按钮我想做

 private void button1_Click(object sender, EventArgs e)
        {
            Argument data=new Argument();
            data.age=Convert.ToInt32(textBox_age.Text);
            char[] name={'a','b','r','a','r', ' ', …
Run Code Online (Sandbox Code Playgroud)

c c# pinvoke interop struct

1
推荐指数
2
解决办法
1万
查看次数

invoke - p/invoke

BeginInvoke/EndInvoke和P/invoke有什么区别?

.net pinvoke invoke

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

在x64构建的应用程序中,"int"的大小是否会发生变化?

可能重复:
x64上的sizeof(int)?

编译为x64与x86时,IntPtr的大小从4变为8.

int的大小是否也会改变,或者它仍然是Int32?

在使用pinvoke和处理互操作调用时,这个问题尤为重要.是否需要将所有"int"类型显式更改为声明为Int32?

c# int 64-bit pinvoke interop

1
推荐指数
2
解决办法
1280
查看次数

方法的类型签名不与PInvoke兼容

我正在尝试使用ac dll中的函数,在ac#应用程序中,每次我尝试运行应用程序并调用有问题的函数时,我都会遇到此错误.
起初我想也许这是因为我正在使用的错误签名,但我试图让它尽可能简单但没有运气.
简而言之:
这是我对c dll的实际源代码:

#include <stdio.h>
#include <string.h>
extern "C"
{
    struct teststruct
    {
        char acharacter;
        int  anumber;
        char* astring;
        char  anarray[10];
        const char* conststring;
    };

    __declspec(dllexport) teststruct  TestDLL()
    {
        teststruct stc;
        stc.acharacter = 'A';
        stc.anumber = 10;
        strcpy(stc.anarray, "Test");
        stc.astring = "astring!";
        stc.conststring = "Crash?";

        return stc;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是c#计数器部分:

    [StructLayout(LayoutKind.Sequential)]
public struct teststruct
{
    public char acharacter;
    public  int anumber;
    [MarshalAs(UnmanagedType.LPStr)]
    public string astring;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public char[] anarray;
    [MarshalAs(UnmanagedType.LPStr)]
    public string conststring;
} …
Run Code Online (Sandbox Code Playgroud)

c# c++ pinvoke marshalling dllimport

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

stdcall调用约定并在C#中使用pinvoke

我创建了一个DLL文件,其中包含两个空函数.

extern "C" __declspec(dllexport) void __stdcall myFunc1() {
    // just empty function
}

extern "C" __declspec(dllexport) void __cdecl myFunc2() {
    // just empty function
}
Run Code Online (Sandbox Code Playgroud)

在C#中,我可以使用DLLImport如下属性调用函数.

[DllImport("myDLL", CallingConvention=CallingConvention.StdCall)]
private extern static void myFunc1();

[DllImport("myDLL", CallingConvention=CallingConvention.Cdecl)]
private extern static void myFunc2();
Run Code Online (Sandbox Code Playgroud)

所以我再次尝试使用LoadLibrary()kernel32.dll而不是DllImport属性.

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void MyFunc1();

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void MyFunc2();
Run Code Online (Sandbox Code Playgroud)

但是,当我调用MyFunc1()工作时MyFunc1()时会发生运行时错误.

所以我__stdcall__cdeclC++ 替换,重新编译DLL,然后在C#中再次调用MyFunc1().

并且......它奏效了.

为什么地球上没有__stdcall呼叫约会在C#中用pinvoke工作?

c# c++ pinvoke calling-convention stdcall

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

PinvokeStackImbalance错误(C#)

我是Pinvoke的新手.

我有一个大型项目调用调用DLL调用vtmail.dll.它在.Net 3.5中运行良好,但在升级到.NET 4.0时我遇到了PinvokeStackImbalance异常

调用代码有点像这样:

VTMail vtMail = new VTMail();
ec = vtMail.SendMail("servername",
   false,
   "",
   "",
   "Subject",
   "Sender Name",
   "sender@mydomain.com",
   "",
   "sendto@theirdomain.com",
   "reply@mydomain.com,
   "Description",
   "foldername",
   false,
   25);
Run Code Online (Sandbox Code Playgroud)

检查vtmail.dll中的声明是这样的:

public int SendMail(string mailServer, bool login, string userName, 
                    string password, string subject, string fromName, 
                    string fromAddress, string toName, string toAddress, 
                    string replyToAddress, string bodyText, string folderToSend, 
                    bool encrypt, long smtpPortNo);
Run Code Online (Sandbox Code Playgroud)

我一直在搜索这个,它似乎是一个已知的.Net 4.0问题.响应总是建议添加CallingConvention=CallingConvention.CdeclDllImport语句中,但是这个特定的调用没有DllImport语句,dll只是作为Visual Studio中项目的引用而添加,因此可用.

我怎样才能解决这个问题?

c# dll pinvoke .net-4.0 visual-studio-2010

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

DEVMODE结构中C#StructLayout.Explicit的对齐错误

我正在尝试使用EnumDisplaySettings,它使用DEVMODE结构作为结果结构。该DEVMODE结构使用了一些工会内部,这使得它更复杂一点在C#中使用。联合用于对显示器或打印机进行编号。StructLayout.Explicit中的 FieldOffsets 应该可以使用工会。

下面是从pinvoke.net复制的结构。显然,其他一些结构也存在问题,只需将其构造为StructLayout.Sequential即可解决联合,并创建了两个结构,一个用于显示,一个用于打印机。

抛出的异常是字段偏移量70,它表明该字段未对齐或与另一个字段重叠。这是我不了解的,当然字段可以与使用的显式布局重叠,并且字段偏移量68之前的字段也很短,不能与字段偏移量70重叠。这就是Microsoft定义的结构的工作方式。当移动从70 fieldoffset到72它的工作原理

因此,我实际上一般不会解决我的问题,但是我对这里发生的情况感兴趣。

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
public struct DEVMODE3
{
    public const int CCHDEVICENAME = 32;
    public const int CCHFORMNAME = 32;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
    [System.Runtime.InteropServices.FieldOffset(0)]
    public string dmDeviceName;
    [System.Runtime.InteropServices.FieldOffset(32)]
    public Int16 dmSpecVersion;
    [System.Runtime.InteropServices.FieldOffset(34)]
    public Int16 dmDriverVersion;
    [System.Runtime.InteropServices.FieldOffset(36)]
    public Int16 dmSize;
    [System.Runtime.InteropServices.FieldOffset(38)]
    public Int16 dmDriverExtra;
    [System.Runtime.InteropServices.FieldOffset(40)]
    public uint dmFields;

    [System.Runtime.InteropServices.FieldOffset(44)]
    Int16 dmOrientation;
    [System.Runtime.InteropServices.FieldOffset(46)]
    Int16 dmPaperSize;
    [System.Runtime.InteropServices.FieldOffset(48)]
    Int16 dmPaperLength;
    [System.Runtime.InteropServices.FieldOffset(50)]
    Int16 dmPaperWidth; …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke interop

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

SendMessage WM_SETTEXT到另一个进程失败,因为在目标进程中收到的指针被更改

我目前正致力于自动化无法更改的Win32 UI应用程序.到目前为止,我的方法是使用目标应用程序的标准消息队列来注入我的输入.我已经走得很远了:

  • 使用WM_COMMAND作品"点击"按钮
  • TCM_GETITEMA通过虚拟鼠标点击WM_LBUTTONDOWN/ WM_LBUTTONUP工作来读取选项卡的名称并激活它们
  • 读取控件的启用/禁用状态有效

然而,我遇到的情况是修改可编辑的ComboBox及其Edit控件的文本.我尝试使用这样的WM_SETTEXT消息:

    public static void SetText(IntPtr hWnd, string text) {

        // Maximum item text buffer length
        const int MAX_LEN = 512;    

        // Get process
        uint ProcessId;
        WinAPI.GetWindowThreadProcessId(hWnd, out ProcessId);

        IntPtr process = WinAPI.OpenProcess(
            WinAPI.ProcessAccessFlags.VMOperation | WinAPI.ProcessAccessFlags.VMRead |
            WinAPI.ProcessAccessFlags.VMWrite | WinAPI.ProcessAccessFlags.QueryInformation,
            false, ProcessId
        );

        if( process == IntPtr.Zero )
            throw new Exception("Could not open process");

        // Allocate memory in remote process
        IntPtr farTextPtr = WinAPI.VirtualAllocEx(process, IntPtr.Zero, MAX_LEN, 
            WinAPI.AllocationType.Commit, …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke winapi

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