I have a dll that works with VB.NET, but I can't get it to work with VB 6.0. Any ideas?
The function in the dll is simple. It's pretty much:
Code:_declspec(dllexport) int _stdcall returnOne()
{
return 1;
}
Printable View
I have a dll that works with VB.NET, but I can't get it to work with VB 6.0. Any ideas?
The function in the dll is simple. It's pretty much:
Code:_declspec(dllexport) int _stdcall returnOne()
{
return 1;
}
Did you add a reference to it in "Project > References..." ?
How did you declare it?
VB Code:
Declare Function returnOne Lib "x:\xx\mydll.dll" () As Long
ok, that function is working now. But I have this other function that "kinda" works; it returns some of the string and adds a extra garbage characters.
This dll function works in VB.Net, but not in VB6. Oh, and i can't reference it in projects->reference; maybe because its win32 dll.
Weird thing, when I tried to make the return type into string (_declspec(dllexport) string _stdcall readRegistry), weird things happen like RegOpenKeyEx doesn't return 0, but when I put the return type back to LPCSTR, RegOpenKeyEx returns 0. Anyways, I followed this code step by step in debug and it returns the right value. But my guess is, its the return type or something in vb6.
Here is the dll code:
and here is the vb6 code:Code:#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 200
char keyVal[BUFSIZE];
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
_declspec(dllexport) LPCSTR _stdcall readRegistry(LPCSTR rootkey, LPCSTR keyx, LPCSTR subkeyx)
{
DWORD dwBufLen=BUFSIZE;
HKEY hKey;
int lRet;
if ( lstrcmpi( "HKEY_CURRENT_USER", rootkey) == 0 )
{
//get open handle
lRet = RegOpenKeyEx( HKEY_CURRENT_USER, keyx, 0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return NULL;
}
else if ( lstrcmpi( "HKEY_CLASSES_ROOT", rootkey) == 0 )
{
//get open handle
lRet = RegOpenKeyEx( HKEY_CLASSES_ROOT, keyx, 0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return NULL;
}
else if ( lstrcmpi( "HKEY_LOCAL_MACHINE", rootkey) == 0 )
{
//get open handle
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, keyx, 0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return NULL;
}
else if ( lstrcmpi( "HKEY_USERS", rootkey) == 0 )
{
//get open handle
lRet = RegOpenKeyEx( HKEY_USERS, keyx, 0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return NULL;
}
else if ( lstrcmpi( "HKEY_CURRENT_CONFIG", rootkey) == 0 )
{
//get open handle
lRet = RegOpenKeyEx( HKEY_CURRENT_CONFIG, keyx, 0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return NULL;
} //get key value
lRet = RegQueryValueEx( hKey, subkeyx, NULL, NULL,
(LPBYTE) keyVal, &dwBufLen);
if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
return NULL;
RegCloseKey( hKey );
return (LPCSTR)keyVal;
}
VB Code:
Private Declare Function readRegistry Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\readReg\Debug\readReg.dll" (ByVal a As String, ByVal b As String, ByVal c As String) As String Private Sub Command1_Click() Dim d As String d = readRegistry("HKEY_USERS", "S-1-5-19\Software\Microsoft\MediaPlayer\Setup\CreatedLinks", "AppName") MsgBox (d) End Sub
Yes they can run in VB6 and any other language that supports API's. Matter of fact, I believe all the dlls located in the System (or System32 if using NT based OS's such as XP) are made with C and C++. If no path is specified, VB automatically searches in those System directories.
And there are different kind of dlls. The one you have is a WinAPI, which is used to call different subs and functions from. They thought you were using an ActiveX dll.
try something like thisVB Code:
Option Explicit Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _ ByVal lpString1 As String, _ ByVal lpString2 As Long _ ) As Long Private Declare Function readRegistry Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\readReg\Debug\readReg.dll" ( _ ByVal a As String, _ ByVal b As String, _ ByVal c As String _ )[color=red] As Long[/color] Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _ ByVal lpString As Long _ ) As Long Private Sub Command1_Click() Dim lpString As Long Dim strVal As String Dim lenstr As Long lpString = readRegistry("HKEY_USERS", "S-1-5-19\Software\Microsoft\MediaPlayer\Setup\CreatedLinks", "AppName") lenstr = lstrlen(lpString) + 1 strVal = String(lenstr, 0) lstrcpy strVal, lpString MsgBox strVal End Sub
thanks moeur. It worked. But i'm wondering, is there a way to change the dll function so that I could simply make the return value for the readRegistry into string? If not, could you tell me why the function can't return a string in vb6, yet it could in vb.net?
You can return a string to VB6 as one of the parameters because then you could declare it ByVal. When you decalre a string ByVal it converts the string between VB and C formats for you.
Only possible if returned as a parameter? Not as a return value of the function? Ok, thanks again.