Results 1 to 9 of 9

Thread: Does win32 dll made from vc++ 6.0 run in VB 6.0?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Does win32 dll made from vc++ 6.0 run in VB 6.0?

    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;
    }

  2. #2
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    Did you add a reference to it in "Project > References..." ?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    How did you declare it?
    VB Code:
    1. Declare Function returnOne Lib "x:\xx\mydll.dll" () As Long

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    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:

    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;
    		
    }
    and here is the vb6 code:

    VB Code:
    1. 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
    2.  
    3. Private Sub Command1_Click()
    4.     Dim d As String
    5.     d = readRegistry("HKEY_USERS", "S-1-5-19\Software\Microsoft\MediaPlayer\Setup\CreatedLinks", "AppName")
    6.     MsgBox (d)
    7. End Sub

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    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.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    try something like this
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _
    4.     ByVal lpString1 As String, _
    5.     ByVal lpString2 As Long _
    6. ) As Long
    7.  
    8. Private Declare Function readRegistry Lib "C:\Program Files\Microsoft Visual Studio\MyProjects\readReg\Debug\readReg.dll" ( _
    9.     ByVal a As String, _
    10.     ByVal b As String, _
    11.     ByVal c As String _
    12. )[color=red] As Long[/color]
    13.  
    14. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
    15.     ByVal lpString As Long _
    16. ) As Long
    17.  
    18. Private Sub Command1_Click()
    19.     Dim lpString As Long
    20.     Dim strVal As String
    21.     Dim lenstr As Long
    22.    
    23.     lpString = readRegistry("HKEY_USERS", "S-1-5-19\Software\Microsoft\MediaPlayer\Setup\CreatedLinks", "AppName")
    24.     lenstr = lstrlen(lpString) + 1
    25.     strVal = String(lenstr, 0)
    26.     lstrcpy strVal, lpString
    27.     MsgBox strVal
    28. End Sub

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    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?

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Does win32 dll made from vc++ 6.0 run in VB 6.0?

    Only possible if returned as a parameter? Not as a return value of the function? Ok, thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width