开发者

Creating DLLs in C++

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>
#defin开发者_StackOverflowe 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.


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 the c:\ 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.


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜