【问题标题】:Unable to create DLLs: Getting DLL "is not a valid Win32 application"无法创建 DLL:获取 DLL“不是有效的 Win32 应用程序”
【发布时间】:2019-01-22 15:54:41
【问题描述】:

正如标题所说,我无法创建一个简单的 DLL。我正在使用 VS 2017 社区版 15.8.0 版。这是.dll代码:

#include "stdafx.h"
#include "InvWin32App.h"

#include "$StdHdr.h"

void Prc1()
{
    printf("ran procedure 1\n");
}

这是标题的代码,按照 MS 的做事方式:

#ifdef INVWIN32APP_EXPORTS
#define INVWIN32APP_API __declspec(dllexport)
#else
#define INVWIN32APP_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

        INVWIN32APP_API  void Prc1();

#ifdef __cplusplus
}
#endif

这里是驱动代码:(更新:驱动程序是一个.exe程序。)

#include "pch.h"
#include "InvWin32App.h"

int main()
{
    Prc1();
}

没有比这更简单的了。我尝试运行代码时收到以下错误消息框:

Unable to start program
program name.dll
program name.dll is not
a valid Win32 application

我可以创建 .exe 程序没问题。今天早上我在运行 VS 2017 版本 15.7.5 时也遇到了错误。升级VS没有任何好处。我也尝试将这些编译为 .c 程序,但没有任何区别。

我曾多次使用 VS 2015 创建 .exe 程序时遇到此问题。我不记得我做了什么,但问题消失了。任何帮助将不胜感激。

TIA。

【问题讨论】:

  • 你不能直接运行dll,通常它们会被动态加载到exe中。
  • Windows 是正确的。您不能直接运行 dll。您可能打算将应用程序(您调用驱动程序代码)设置为启动项目而不是 dll。
  • 谢谢。我没有直接运行 .dll。我使用驱动程序代码,这是一个.exe来调用.dll。
  • 您的错误信息似乎与此冲突。其中哪个项目是启动项目?
  • 我今天实在是太傻了。启动程序是 .dll,而不是 .exe。非常感谢,因为它解决了这个问题。

标签: c++ c visual-studio


【解决方案1】:

在解决方案资源管理器中右键单击作为可执行文件的项目,然后单击“设置为启动项目”。

请注意,“不是有效的 Win32 应用程序”不是编译错误或链接错误,它是您尝试调试不可执行的内容时收到的消息。

您只能启动可执行文件。可执行文件消耗 dll。这应该是两个独立的项目,有两组对应的项目设置。

【讨论】:

  • 不过,您可以在 Visual Studio 中调试 dll。打开你的 dll 项目的属性Project > Properties,转到Configuration Properties > Debugging 选项卡,将Attach 字段设置为Yes。在Command 字段中设置可执行文件,您将附加您的.dll。按ApplyOk 按钮。打开Build > Configuration Manager 菜单。将Active Solution Configuration 设置为Debug。现在您可以启动目标可执行文件并开始调试您的 .dll。当您按下Debug > Start Debugging 时,Visual Studio 会将自己附加到目标进程并查找您的 dll。
  • @KulaGGin 你认为 OP 做了所有这些,遇到了他描述的错误,然后来这里问他问的问题吗?我不。但是,如果您要求进行编辑以提高清晰度,请告诉我您的建议。如果您的评论只是仅供参考,那也很酷。
  • 是的,对于那些在尝试调试 dll 时会遇到此错误的人来说,这只是一个仅供参考。我今天试图调试一个 dll 并开始收到此错误。在我的情况下,这是因为我切换到 Release 构建配置,并通过切换回 Debug 构建配置来修复它。
【解决方案2】:

我怀疑您使用的是 32 位 dll。如果你有你的 64 位 Windows 操作系统,那么你也必须有这个 dll 的 64 位版本。有时,即使您已经拥有 64 位 dll,它也无法运行,因此请尝试此 dll 的其他版本,即此 dll 的 32 位版本。 除此之外,还要对您的 unicurse.py 文件进行一些小改动。 PFB要添加的代码:

import ctypes
pdlib = ctypes.CDLL("pdcurses.dll")

虽然这行代码在某些条件下,但是放在上面可以帮助您检查 dll 是否已加载。

【讨论】:

    【解决方案3】:

    对于此时任何入党的人,我发现这个错误是你尝试运行32bit dll using a 64bit python时引起的。

    现在我不会详细说明为什么这不起作用,因为上面的回答清楚地描述了这一点,但是有两种解决方案。

    1. 安装 32 位 python 并使用它与 dll 进行通信。
    2. 使用 msl-loadlib 库的进程间通信documentation

    我使用了似乎更可持续的第二步。我在下面使用的步骤可以在上面的文档链接中找到。

    • 首先是创建一个与32位模块通信的服务器模块
    #server.py
    from msl.loadlib import Server32
    
    
    class Server(Server32):
        # the init takes mandatory host and port as arguments
        def __init__(self, host, port, **kwargs):
            # using windll since this application is being run in windows, other options such as cdll exists
            # this assumes that the dll file is in the same directory as this file
            super(Server, self).__init__('tresdlib.dll', 'windll', host, port)
        
        # define a function that is to be called with the required arguments
        def fsl_command(self, com, doc):
            #the server32 exposes the loaded dll as lib, which you can then use to call the dll functions and pass the required arguments
            return self.lib.FSL_Command(com,doc)
    
    • 然后我们创建一个客户端模块,将 python 请求发送到服务器模块
    #client.py
    from msl.loadlib import Client64
    
    
    class Client(Client64):
    
        def __init__(self):
            # pass the server file we just created in module32=, the host am using is localhost, leave port as none for self assigning
            super(Client, self).__init__(module32='server', host="127.0.0.1", port=None)
    
        # define a function that calls the an existing server function and passes the args
        def fsl_command(self, com, doc):
            return self.request32('fsl_command', com, doc)
    
    

    我们将在终端中运行它,但您可以选择在另一个应用程序中调用

    >>> from client import Client
    >>> c = Client()
    >>> c.fsl_command(arg1,arg2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 2012-03-05
      • 1970-01-01
      • 2012-11-07
      • 2014-01-02
      相关资源
      最近更新 更多