![](/img/trans.png)
[英]problem with c++ pointers in visual studio (but works in g++)
[英]C++: dll not working on Visual Studio 2017 but works with g++
我在使用更復雜的代碼時遇到了這個問題,這自然是一個簡化的示例。
dll 不適用於 VS,但適用於 g++,
我有這個代碼,
#include <stdio.h>
extern "C" __declspec(dllexport) void func(int a)
{
printf("a: %d\n", a);
}
這是我的 VS 項目中唯一的文件。 我編譯它,VS 成功生成了一個 test.dll
但是當我調用庫時(例如來自 python),它抱怨 dll 不是 win32,看,
import os
import sys
import ctypes
import sys
file_dll = 'test.dll'
print('Using {}...'.format(file_dll),flush=True)
lib = ctypes.cdll.LoadLibrary(file_dll)
a=ctypes.c_int(0)
lib.func(a)
輸出,
Using ./test.dll...
Traceback (most recent call last):
File "call.py", line 14, in <module>
lib = ctypes.cdll.LoadLibrary(file_dll)
File "..\anaconda3\lib\ctypes\__init__.py", line 434, in LoadLibrary
return self._dlltype(name)
File "..\anaconda3\lib\ctypes\__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
現在,如果我使用編譯相同的代碼,
g++ test.cpp -shared -o test2.dll
輸出,
Using test2.dll... a: 0
任何人都知道我做錯了什么?
錯誤“不是有效的 Win32 應用程序”通常表示位數不匹配,64 位進程試圖加載 32 位庫,反之亦然。 鑒於發布的屏幕截圖顯示 DLL 是 32 位(平台 = x86),python 模塊很可能是 64 位。
錯誤消息的“ Win32 應用程序”部分沒有提到引用的模塊是 32 位的(在本例中確實是)。 相反,“ Win32 應用程序”是(本機)Windows 應用程序的技術術語,無論 Windows 主機或客戶端應用程序的位數如何。 從Win32 (Windows API)引用 MS:
Win32 API(也稱為 Windows API)是 Windows 應用程序的原生平台。 這個 API 最適合需要直接訪問系統功能和硬件的桌面應用程序。 Windows API 可用於所有桌面應用程序,32 位和64 位Windows 普遍支持相同的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.