想用vb做一个标准的DLL供JAVA来调用,但是在用vb来测试这个DLLD时出错,思路如下,请高手指点:一、在vb中建立一个activex dll,再新建空模块module1和类模块class1,class1中代码如下:Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As LongConst MAX_LEN = 200 '字符串最大长度
Const DESKTOP = &H0& '桌面
Const PROGRAMS = &H2& '程序集
Const MYDOCUMENTS = &H5& '我的文档
Const MYFAVORITES = &H6& '收藏夹
Const STARTUP = &H7& '启动
Const RECENT = &H8& '最近打开的文件
Const SENDTO = &H9& '发送
Const STARTMENU = &HB& '开始菜单
Const NETHOOD = &H13& '网上邻居
Const FONTS = &H14& '字体
Const SHELLNEW = &H15& 'ShellNew
Const APPDATA = &H1A& 'Application Data
Const PRINTHOOD = &H1B& 'PrintHood
Const PAGETMP = &H20& '网页临时文件
Const COOKIES = &H21& 'Cookies目录
Const HISTORY = &H22& '历史
Public Function getpath(ByVal mycom As String) As String
Dim sTmp As String * MAX_LEN '存放结果的固定长度的字符串
Dim nLength As Long '字符串的实际长度
Dim pidl As Long '某特殊目录在特殊目录列表中的位置Select Case mycomCase "windows"
'*************************获得Windows目录**********************************
length = GetWindowsDirectory(sTmp, MAX_LEN)
getpath = Left(sTmp, length)
Case "system"
'*************************获得System目录***********************************
length = GetSystemDirectory(sTmp, MAX_LEN)
getpath = Left(sTmp, length)
Case "temp"
'*************************获得Temp目录***********************************
length = GetTempPath(MAX_LEN, sTmp)
getpath = Left(sTmp, length)
Case "desktop"
'*************************获得DeskTop目录**********************************
SHGetSpecialFolderLocation 0, DESKTOP, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "sendto"
'*************************获得发送到目录**********************************
SHGetSpecialFolderLocation 0, SENDTO, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "mydocuments"
'*************************获得我的文档目录*********************************
SHGetSpecialFolderLocation 0, MYDOCUMENTS, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "programs"
'*************************获得程序集目录***********************************
SHGetSpecialFolderLocation 0, PROGRAMS, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "startup"
'*************************获得启动目录*************************************
SHGetSpecialFolderLocation 0, STARTUP, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "startmenu"
'*************************获得开始菜单目录*********************************
SHGetSpecialFolderLocation 0, STARTMENU, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "favorites"
'*************************获得收藏夹目录***********************************
SHGetSpecialFolderLocation 0, MYFAVORITES, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "recent"
'**********************获得最后打开的文件目录*******************************
SHGetSpecialFolderLocation 0, RECENT, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "nethood"
'*************************获得网上邻居目录*********************************
SHGetSpecialFolderLocation 0, NETHOOD, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "fonts"
'*************************获得字体目录**********************************
SHGetSpecialFolderLocation 0, FONTS, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "cookies"
'*************************获得Cookies目录**********************************
SHGetSpecialFolderLocation 0, COOKIES, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "history"
'*************************获得历史目录**********************************
SHGetSpecialFolderLocation 0, HISTORY, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "pagetmp"
'***********************获得网页临时文件目录*******************************
SHGetSpecialFolderLocation 0, PAGETMP, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "shellnew"
'*************************获得ShellNew目录*********************************
SHGetSpecialFolderLocation 0, SHELLNEW, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "appdata"
'***********************获得Application Data目录*****************************
SHGetSpecialFolderLocation 0, APPDATA, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)
Case "printhood"
'*************************获得PrintHood目录*********************************
SHGetSpecialFolderLocation 0, PRINTHOOD, pidl
SHGetPathFromIDList pidl, sTmp
getpath = Left(sTmp, InStr(sTmp, Chr(0)) - 1)Case Else
getpath = "没有该目录"End Select
End FunctionPublic Function getlinkpath(ByVal filename As String) As String Dim Obj As Object
 Set Obj = CreateObject("WScript.Shell")
 Dim Shortcut As Object
 Set Shortcut = Obj.CreateShortcut(filename)
 getlinkpath = Shortcut.TargetPath
 Shortcut.SaveEnd Function
二、通过网上的中断生成dll的方法,在编译生成DLL过程中获得class1.obj,getpath.obj,module1.obj三个文件。复制到c:\aaa目录下,我的vb装在c盘默认目录下。三、用批处理手工编译生成DLL文件。vb原始自带的连接程序为link.exe,批处理by.bat内容如下:
cd C:\Program Files\Microsoft Visual Studio\VB98
link.exe "c:\aaa\Class1.obj" "c:\aaa\Module1.obj" "c:\aaa\getpath.obj" "c:\Program Files\Microsoft Visual Studio\VB98\VBAEXE6.LIB " /ENTRY:__vbaS /EXPORT:getpath /EXPORT:getlinkpath /OUT:"c:\aaa\getpathok.dll" /BASE:0x11000000 /SUBSYSTEM:WINDOWS,4.0 /VERS 四、成功在c:\aaa\下生成getpathok.dll,在vb中新建EXE工程,一个窗体form1;窗体上两个文本框text1,text2;两个按钮command1,command2;代码如下:Option Explicit
Private Declare Function getpath Lib "getpathok.dll" (ByVal mycom As String) As String
Private Declare Function getlinkpath Lib "getpathok.dll" (ByVal filename As String) As StringPrivate Sub Command1_Click()
  'MsgBox getpath(trim(text1.text))
   MsgBox getpath("sendto")
End SubPrivate Sub Command2_Click()
  MsgBox getlinkpath("a.lnk")
End Sub五、把第四步建立的工程生成test.EXE文件,放到c:\aaa目录下,再拷贝IE快捷方式a.lnk到c:\aaa目录下,运行test.exe。text.exe出错自动关闭。注册getpathok.dll文件时提示失败,返回代码0xc0000005。个人觉得应该是class1中代码有问题,因为用网上的实例(class1中代码为空,module1中有两个简单的函数,注册DLL时也提示失败,返回代码一样,但是在vb中调用时可以成功通过)恳请高手指点。

解决方案 »

  1.   

    activex dll不是标准的DLL,做标准的还是用C吧,VB做不了
      

  2.   

    记得那位大牛用VB实现过,不过要改OBJ文件的链接。比C还麻烦!!
    且不论obj文件在一般情况下是获得不了的。
      

  3.   

    vb不直接支持做一个标准的DLL
      

  4.   

        Create in Visual Basic version 2
       (c) 2004 DanSoft Australia - http://dansoftaus.r8.org/
       [MAXIMIZE NOTEPAD FOR BEST VIEWING EXPERIENCE ;)]NOTE: If you used the first version of my 'Make DLL's in VB' utility, you need to uninstall it.
          See the instructions at the bottom.'Visual Basic directory' refers to the folder that Visual Basic is installed in, usually
    'c:\Program Files\Microsoft Visual Studio\VB98'Here are the instructions to install this utility:0) Unzip the zip file -- Remember to extract paths!
    1) Open 'Linker.vbp' (in the 'linker' folder) and compile it.
    2) Copy the 'MakeDLL.exe' file (in the 'compiled' folder) to your Visual Basic directory.
    3) Open 'MakeDLLAddin.vbp' (in the 'addin' folder) and compile it/if allready compile then register it.
    4) Go into Visual Basic, and click Add-Ins -> Add-In Manager. There should be an addin listed
       called 'Create DLLs In Visual Basic' (or similar). Make sure both 'Loaded' and 
       'Load On Startup' are ticked.
    4a) If the addin wasn't listed, copy 'MakeDLL.DLL' (in the 'compiled' folder) into your 
        Visual Basic directory and restart Visual Basic.
    5) Copy all the files in the 'dll project' folder to your Visual Basic Project Templates folder
       (usually C:\Program Files\Microsoft Visual Studio\VB98\template\projects)Yay! It is now installed! * If you want to create a DLL yourself, go into Visual Basic and choose to create a 
      'Standard DLL' project.
    * To choose what functions you want to export into your DLL file, click File -> Choose
      DLL Exports...* To create your DLL, click File -> Make xxxxxxx.dll (this might show as 'Make xxxxxxx.exe', 
      when the save box pops up, just change the extension to .DLL)Sample DLL: 'TestDLL.vbp' (in the 'test dll' folder)
    Sample prog that uses that DLL: 'TestProg.vbp' (in the 'test program' folder)If you want to try the Sample program, build the Sample DLL into the 'test program' folder
    and then run the sample program.
    Old Version Uninstall:
    To uninstall version 1 of my Make DLLs in VB utility, follow these steps:1) Go into your Visual basic directory (C:\Program Files\Microsoft Visual Studio\VB98)
    2) Delete 'LINK.EXE'
    3) Rename 'LINK1.EXE' to 'LINK.EXE'
    4) Delete 'MakeDLL.DLL' if you know where you put it (this step can be skipped if you don't 
       know where the file is)
    5) Follow installation instructions above to install this versionIf you liked this, please vote! (Even if you didn't like it, post comments saying why).
    (c) 2004 DanSoft Australia - http://dansoftaus.r8.org/
      

  5.   

    http://download.csdn.net/source/3261993 上传上去了 嘿嘿
      

  6.   

    amicForVB
    vb6的一个插件支持导出函数与入口(dllmain)设置
    菜单->工程->属性->扩展
    挺方便的.