在 C++ 中创建 DLL
这是这个问题的后续问题,因为我失去了理智现在这个。
有人向我指出这篇文章,我正在尝试从那里复制第 4 节。
因此,我在 MSVC++2010 中创建了一个空的 C++ 项目,在其中创建了一个新的 .cpp 文件,并将以下代码放入其中:(
#include <windows.h>
#define CCONV _declspec(dllexport) // used to be __stdcall but resulting DLL is identical
int CALLBACK LibMain (HANDLE hInstance, WORD wDataSeg, WORD wHeapSize,
LPSTR lpszCmdLine)
{
return 1;
}
short CCONV PassInteger (short intgr, short far *pintgr)
{
*pintgr = intgr;
return intgr + 1;
}
我从 这里但我认为它在这里没有做任何事情。)
然后,我添加了一个 .def 文件项目并将其放入其中:
;vb6dll32 DEF File
LIBRARY vb6dll32
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE
EXPORTS
PassInteger
编译器输出两个警告,当前目标不支持.def文件中的CODE
和DATA
,但它最终编译并生成文件 vb6dll32.dll
然后我将其复制到 C:\windows\system
和 ...\system32
和 C: \。
然后我创建了一个 VB6 项目,在表单中放入一个按钮并添加了以下源代码:
Private Declare Function PassInteger Lib "vb6dll32.dll" _
(ByVal intgr As Integer, pintgr As Integer) As Integer
Private Function BuiltIntest() As Integer
Dim i As Integer
Dim ir As Integer
i = 7
i = PassInteger(i, ir)
Print i, ir
Return
End Function
Private Sub Command1_Click()
MsgBox (BuiltIntest())
End Sub
现在,当我单击该按钮时,它仍然给出“运行时错误'53':找不到文件 vb6dll32.dll”。 (即使我在 VB 源代码中给它一个完全指定的路径,例如“C:\vb6dll32.dll”,并且该文件肯定在那里,也会发生这种情况。我尝试给出它的位置,没有路径,也没有“.dll”等等,什么都没有改变。)
同样让我烦恼的是,当我运行 regsvr32 c:\vb6dll32.dll 时,它还告诉我“模块 C:\vb6dll32.dll 无法加载。等等”。我不知道它应该做什么,但通常应该对 DLL 文件做一些事情,对吗?
我做错了什么?!感谢您的帮助。
This is a follow-up question to this question because I'm losing my mind over this right now.
Someone pointed me to this article and I'm trying to copy section 4 from there.
So I created an empty C++ Project in MSVC++2010, created a new .cpp file inside it, and put the following code in there:
#include <windows.h>
#define CCONV _declspec(dllexport) // used to be __stdcall but resulting DLL is identical
int CALLBACK LibMain (HANDLE hInstance, WORD wDataSeg, WORD wHeapSize,
LPSTR lpszCmdLine)
{
return 1;
}
short CCONV PassInteger (short intgr, short far *pintgr)
{
*pintgr = intgr;
return intgr + 1;
}
(I got the LibMain
code from here but I think it doesn't do anything here.)
Then, I added a .def file to the project and put this in it:
;vb6dll32 DEF File
LIBRARY vb6dll32
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE
EXPORTS
PassInteger
The compiler outputs two warnings that CODE
and DATA
in the .def file are not supported for the current target, but it eventually compiles and generates the file vb6dll32.dll
which I have then copied to C:\windows\system
and ...\system32
and C:\
.
Then I have created a VB6 project, put a button into the form and added this sourcecode:
Private Declare Function PassInteger Lib "vb6dll32.dll" _
(ByVal intgr As Integer, pintgr As Integer) As Integer
Private Function BuiltIntest() As Integer
Dim i As Integer
Dim ir As Integer
i = 7
i = PassInteger(i, ir)
Print i, ir
Return
End Function
Private Sub Command1_Click()
MsgBox (BuiltIntest())
End Sub
Now, when I click the button, it still gives me "Runtime error '53': file vb6dll32.dll not found." (This happens even if I give it a fully specified path in the VB source code, e.g. "C:\vb6dll32.dll" and the file is definitely there. I tried giving its location without path and without ".dll" and so on, nothing changes.)
What also bugs me is, when I run regsvr32 c:\vb6dll32.dll
it also tells me "The module C:\vb6dll32.dll could not be loaded. etc etc" .. I have no idea what it should do but that should generally do something for DLL files, right?
What am I doing wrong?! Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的示例(代码和 regsvr32 调用)中,您指的是
c:\vb6dll32.dll
。但你说你把它复制到system32目录下。您指定的路径要求它位于根目录中。由于您将其复制到 system32 目录,因此它应该可以在没有任何路径的情况下工作。尝试从名称中删除c:\
。编辑由于我在各方面都出类拔萃,您可能会犹豫是否要遵循我的建议......但是Dependency Walker 可能有助于解决这个问题。可能是没有找到您的 DLL 所需的 DLL(例如,CRT DLL 之一)。 dependent.exe 实用程序是一个非常有用的工具,它将显示是否缺少任何必要的 DLL。
In your examples (code and regsvr32 call), you are referring to
c:\vb6dll32.dll
. But you said you copied it to the system32 directory. The path you specified would require that it be in the root. Since you copied it to the system32 directory, it should work without any path. Try removing thec:\
from the name.Edit Since I'm striking out on all fronts, you might be hesitant to follow my advice ... but Dependency Walker may help solve this. It could be that a DLL needed by your DLL is not being found (e.g., one of the CRT DLLs). That depends.exe utility is a very useful tool and will show if any necessary DLLs are missing.
也许您的 vb6dll32.dll 是动态链接的并且依赖于诸如 MSVCR100.dll 之类的东西,这
不容易找到。检查导入并将这些 dll 放在 vb6dll32.dll 旁边,或者
静态链接它(/MT /LD)。
另外,您确实不需要 .def 文件中的这些 CODE 和 DATA 行。
Maybe your vb6dll32.dll is dynamically linked and depends on stuff like MSVCR100.dll, which
isn't easy to locate. Check imports and put these dlls beside your vb6dll32.dll, or
link it statically (/MT /LD).
Also you really don't need these CODE and DATA lines in the .def file.
LibMain 来自 16 位 Windows。您应该使用 DllMain。 http://msdn.microsoft.com/en -us/library/ms682583(v=vs.85).aspx。您可以针对 DLL_PROCESS_ATTACH 查看 fdwReason
DLL_PROCESS_DETACH 用于使用 LoadLibrary 加载和卸载 dll 时。对于在 DllMain 中可以使用的内容有一些规定,例如不能使用非托管代码。
LibMain is from 16bit windows. You should use DllMain. http://msdn.microsoft.com/en-us/library/ms682583(v=vs.85).aspx. You can look at fdwReason against DLL_PROCESS_ATTACH
and DLL_PROCESS_DETACH for when the dll is loaded and unloaded with LoadLibrary. There are some stipulations to what you can use in DllMain, for example no unmanaged code.