标签: pinvoke

驱动器上剩余的可用空间 - WinAPI - Windows CE

我已经忘记了WinAPI调用,以了解特定驱动器上剩余的空间,并且pinvoke.net没有给我任何爱.顺便说一句,这是紧凑的框架,所以我想coredll.dll.

有没有更好记忆的人可以开玩笑吗?

pinvoke winapi diskspace windows-ce

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

如何通过Pinvoke传递指向结构的指针?

我正在尝试编写与以下内容等效的C#:

typedef struct BATT_ID
{
    UINT8       nBattID[8];
} BATT_ID, *PBATT_ID;

HANDLE  g_hDevice;

// Connect to the driver
g_hDevice = CreateFile(L"BAT1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

void GetBattID(PBATT_ID pBattId)
{
    // ... snipped code to check g_hDevice is valid ...

    DeviceIoControl(g_hDevice, SOMO650_PWR_GET_BATT_ID, NULL, 0, pBattId, sizeof(BATT_ID),  dwByteReturn, NULL))
}

// once BATT_ID has been filled it can be formatted as follows
wsprintf(strInfo, TEXT("%02X:%02X:%02X:%02X:%02X:%02X"), BattID.nBattID[6], BattID.nBattID[5], BattID.nBattID[4], BattID.nBattID[3], BattID.nBattID[2], BattID.nBattID[1]);
Run Code Online (Sandbox Code Playgroud)

代码连接到Windows Mobile设备的电源驱动程序并尝试检索电池ID.
这是针对SoMo650和Socket的最新ROM版本,只能在C中提供示例代码.

除了调用DeviceIoControl,我可以成功地完成所有事情(尽我所知),因为我不知道如何将BATT_ID结构转换为C#.

我猜这是因为它是一个结构,而DeviceIoControl期望一个指针我应该看着Marshal.PtrToStructure(),但我的C经验非常少,感觉非常深刻.

任何协助将不胜感激.

c# pinvoke compact-framework marshalling

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

如何编组C#中的结构数组?

我必须在C#中调用C++ DLL.并且dll的标题如下(简化):

// C++的标题

struct vector
{
    float x;
    float y;

    vector()
    {}

    vector(float x0, float y0)
    {
        x = x0;
        y = y0;
    }
};

struct unmanaged_struct
{
    int int_var;
    float float_var;
    char* chars_var;
    vector vector_var;

    unmanaged_struct(int i, float f, char* ch, float vec_x, float vec_y) 
    {
        int_var = i;
        float_var = f;
        chars_var = ch;
        vector_var = vector(vec_x, vec_y);
    }
};
Run Code Online (Sandbox Code Playgroud)

//此函数用于输出struct实例的所有变量值

extern "C" __declspec( dllexport )  void unmanagedstruct_summary(unmanaged_struct* us_list, int length);
Run Code Online (Sandbox Code Playgroud)

我在C#中定义了以下类

// CSHARP

[StructLayout(LayoutKind.Sequential)]
public class …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke

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

SetTimeZoneInformation不会为另一个.NET应用程序更新DateTime.Now

以下是我更改TimeZoneInfo(App#1)的方法:

private static void ChangeTimeZone(TimeZoneInfo tzi)
{
    TIME_ZONE_INFORMATION actual = new TIME_ZONE_INFORMATION();
    NativeMethods.GetTimeZoneInformation(out actual);

    if (tzi == null || actual.StandardName == tzi.StandardName)
        return;

    TIME_ZONE_INFORMATION newZone = (TIME_ZONE_INFORMATION)tzi;

    RunWin32Method(() => NativeMethods.SetTimeZoneInformation(ref newZone));

    // Update .NET
    CultureInfo.CurrentCulture.ClearCachedData();
    TimeZoneInfo.ClearCachedData();

    // Notify all windows that we changed a Windows setting.
    // result is True
    IntPtr ptr;
    System.Diagnostics.Debug.WriteLine(NativeMethods.SendMessageTimeout(NativeMethods.HWND_BROADCAST, NativeMethods.WMI_SETTING_CHANGE,
        IntPtr.Zero, IntPtr.Zero, 0x00, 1000, out ptr));
}
Run Code Online (Sandbox Code Playgroud)

当我打电话给我的方法:

ChangeTimeZone(TimeZoneInfo.GetSystemTimeZones().First(e => !e.SupportsDaylightSavingTime));
// Stopping debugger and watching other .NET App then continue to next instruction
ChangeTimeZone(TimeZoneInfo.GetSystemTimeZones().First(e => …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke

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

如何修复"检测到Pinvoke堆栈不平衡"错误

我正在尝试使用PostMessage将密钥发送到窗口.但是在我的程序发送密钥后,我在调试窗口中收到错误.

出现的错误是:检测到PInvoke堆栈不平衡.

我想我错误地调用了DLL:

[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, Int64 lParam);
Run Code Online (Sandbox Code Playgroud)

c# pinvoke

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

从Delphi DLL发送二进制数据做C#应用程序

我有一个Delphi 2010 DLL,用于压缩C#APP中的一些数据.DLL函数如下所示:

function CompressString(aInputString: PAnsiChar; aInputStringSize: Integer; 
  var aOutPutString: PAnsiChar; var aOutPutStringSize: Integer; 
  var aErrorMsgBuffer: PAnsiChar; var aErrorMsgBufferSize: integer): Integer; 
  stdcall; export;
Run Code Online (Sandbox Code Playgroud)

C#方法如下所示:

[DllImport("MyDLL.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi)]
public static extern int CompressString(string aInputString, 
  int aInputStringSize, ref string aOutPutString, 
  out int aOutPutStringSize, ref string aErrorMsgBuffer, 
  out int aErrorMsgBufferSize);
Run Code Online (Sandbox Code Playgroud)

我的问题是aOutPutString被截断,只有部分数据被C#App看到.如果我aOutPutString将Delphi DLL内部更改为一个简单的文字常量(仅用于测试),它可以正常工作.

在DLL内部,我正在使用字符串.在函数的最后,我打电话给:

StrPCopy(aOutPutString, vOutOutAnsiStr);
Run Code Online (Sandbox Code Playgroud)

转换一个AnsiStringPAnsiChar.

我想我不应该使用PAnsiChar,但一个array of byte,但我不知道该怎么做.

c# delphi pinvoke delphi-2010

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

以编程方式单击"消息框"按钮

正如标题所示,我试图以编程方式模拟MessageBox中的按钮单击.我之前尝试通过其标题找到其句柄,并应用WM_CLOSESC_CLOSE在中关闭MessageBox SendMessage().但是,由于存在"是/否"按钮,这不起作用(X按钮呈灰色显示).

现在我想点击No按钮,如下所示 - :

List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
     IntPtr Window_hWnd = CloseMessageBox.FindWindowByCaption("#32770", "LastQuestion"); //Could use null as the first argument too. "#32770" represents classname Dialog.
     CloseMessageBox.EnumChildWindows(Window_hWnd, (hWnd, lParam) =>
     {
          StringBuilder sb = new StringBuilder();
          foreach (var control in GCHandle.FromIntPtr(lParam).Target as List<IntPtr>)
          {
                CloseMessageBox.GetWindowText(control, sb, 250);
                if (sb.Equals("&No"))
                {
                    CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseDown, 0, 0);
                    CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseUp, 0, 0);
                }
          }
          return false;
     }, GCHandle.ToIntPtr(listHandle));

 }
 catch (Exception e) …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke findwindow

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

将新窗口添加到桌面时是否触发任何事件

我想知道当桌面上出现/出现新窗口时是否有任何事件被触发.我愿意使用COM,WMI,WinApis,UIAutomation或任何其他方法,但选择的语言是C#.

实际要求:一个进程有一个主窗口和许多其他窗口.其中一个窗口的类名称是X,(我使用pinvoke获取此信息).现在,只要进程中有通知,此窗口就会弹出一些时间.我不想显示这个窗口.我没有对该进程的代码访问权限,因此我可以禁用该窗口.那么有什么方法可以让我得到一个事件或任何其他机制来跟踪桌面,并且任何时候一个带有类名X的窗口来/它将隐藏它.

请问我是否对这个问题不清楚.谢谢

编辑:西蒙的回答非常好.我试过了,并且能够获得所有窗口的通知,除了通知/吐司窗口,例如lync的im toast通知或outlook新邮件通知.我尝试使用自动化元素和Windows模式的不同元素,但仍然无法获得那些...任何想法我怎么能得到那些...你可以阅读西蒙的答案中的评论更多的背景/细节.再次感谢西蒙介绍了UIAUtomation的强大功能 ......爱它!

c# windows com wmi pinvoke

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

将C#回调函数传递给托管和非托管C++库

我想创建一个C#程序,调用托管C++ DLL,然后在本机C++ DLL中执行功能.作为此调用的一部分,我想提供一个可以由本机C++ dll调用的C#回调函数.

C#代码将调用一些托管C++代码,这些代码将调用以下本机C++代码; 我希望本机C++代码调用我的C#回调cb:

int dobatch(CString str)
{
    // I want to to call c#
    if (cb)
        return(cb(str)); // this would execute the function passed from c#.
}
Run Code Online (Sandbox Code Playgroud)

任何想法......我似乎无法让C#回调与dobatch()通过托管C++ dll 调用本机C++ 函数混合.

c# pinvoke

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

我不断收到“无法在DLL'user32.dll'中找到名为'GetWindowLongPtrA'的入口点”

我正在尝试使用,GetWindowLongPtrA但我一直收到“无法在DLL'user32.dll'中找到名为'GetWindowLongPtrA'的入口点”。(也会SetWindowLongPtrA出现相同的错误)。我已经尝试了许多在Google上找到的解决方案,但是他们没有解决。

这是我编写的函数的声明:

[DllImport("user32.dll")]
public static extern IntPtr GetWindowLongPtrA(IntPtr hWnd, int nIndex);
Run Code Online (Sandbox Code Playgroud)

尝试将put EntryPoint = "GetWindowLongPtrA",更改GetWindowLongPtrAGetWindowLongPtr,put CharSet = CharSet.Ansi,切换为GetWindowLongPtrWwith CharSet = CharSet.Unicode等,它们都不起作用。

我的计算机正好是“ 64位”(但是不能调用该64位WinAPI函数吗?)。操作系统是Windows 10。

[1]:https://i.stack.imgur.com/3JrGw.png

但是我的系统驱动器的可用空间不足。这可能是原因吗? 在此处输入图片说明

这个问题有什么解决方案?

.net c# pinvoke winapi getwindowlong

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