GetWindowTextLength 和 GetClassName 可重用 class 在新项目上失败?

GetWindowTextLength and GetClassName reusable class failed on new project?

我 运行 在我的一个解决方案中遇到了一个非常奇怪的行为,我需要帮助来解决这个问题。 我在 Visual Studio 2015 年使用 C#。

我有一个 Class 库项目,其中包含以下内容:

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
static extern long GetClassName(IntPtr hwnd, StringBuilder lpClassName, long nMaxCount);

string getHWNDCaption(IntPtr hwnd)
{
    if (hwnd == IntPtr.Zero) throw new Exception("getHWNDCaption: Invalid pointer!");
    string caption = "";
    StringBuilder windowText = null;
    try
    {
        int max_length = GetWindowTextLength(hwnd);
        windowText = new StringBuilder("", max_length + 50);
        GetWindowText(hwnd, windowText, max_length + 2);
    .....

string getHWNDClassName(IntPtr hwnd)
{
    if (hwnd == IntPtr.Zero) throw new Exception("ExternalWindowsInfo not initiated!");
    string className = "";
    StringBuilder classText = null;
    try
    {
        int cls_max_length = 1000;
        classText = new StringBuilder("", cls_max_length + 5);
        GetClassName(hwnd, classText, cls_max_length + 2);
    .......

在一个旧的 windows 表单项目中,我执行了这些功能,它们 return 需要的数据。

我试图将新的 windows 表单项目添加到同一个解决方案,但在执行相同的功能时,我收到以下错误,我无法解决这个问题运行:

 A call to PInvoke function ...::GetWindowTextLength' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

因为我使用相同的代码,所以我相信这是项目定义中的内容,但无法找出是什么。 任何帮助将不胜感激!

实际上我还有 运行 多一点,感谢 Matthew Watson 提供线索:

  1. 我保留调用约定 = CallingConvention.Cdecl
  2. 我在项目属性 -> 构建中强制项目使用平台 x64(实际上取消选中 "Prefer 32-bit" 没问题,但我想确定)。

谢谢!