<p>现在一切正常:)总结你的文章:</p>
用C++编写DLL:</P>
<pre><code>// Header
extern "C"
{ // Name in DLL will be "MyAdd" - but you won't be able to find parameters etc...
__declspec(dllexport) int MyAdd(int a, int b);
}
// Name will be with lot of prefixes but some other info is provided - IMHO better approach
__declspec(dllexport) int MyAdd2(int a, int b);
//.cpp Code
__declspec(dllexport) int MyAdd(int a, int b)
{ return a+b;
}
__declspec(dllexport) int MyAdd2(int a, int b)
{ return a+b;
}
</code></pre>
<p>然后可以使用program link.exe查看dll中的实际函数名。
link.exe在MSVC2010中的示例如下:</p>
<pre><code>c:\program files\microsoft visual studio 10.0\VC\bin\link.exe
</code></pre>
<p>使用:</p>
<pre><code>link /dump /exports yourFileName.dll
</code></pre>
<p>你看到的是:</p>
<pre><code>ordinal hint RVA name
1 0 00001040 ?MyAdd2@@YAHHH@Z = ?MyAdd2@@YAHHH@Z (int __cdecl MyAdd2(int,int))
2 1 00001030 MyAdd = _MyAdd
</code></pre>
<p>然后在python中,您可以将其导入为:</p>
<pre><code>import ctypes
mc = ctypes.CDLL('C:\\testDll3.dll')
#mc.MyAdd2(1,2) # this Won't Work - name is different in dll
myAdd2 = getattr(mc,"?MyAdd2@@YAHHH@Z") #to find name use: link.exe /dump /exports fileName.dll
print myAdd2(1,2)
#p1 = ctypes.c_int (1) #use rather c types
print mc[1](2,3) # use indexing - can be provided using link.exe
print mc.MyAdd(4,5)
print mc[2](6,7) # use indexing - can be provided using link.exe
</code></pre>