VC++ .NET 2002: Dynamically Load DLL program got Error C2664

G

Guest

Hi all
I copied the following VC++ 6.0 code (written by someone in the website) to my VC++ .Net 2002-Windows XP Pro PC
////****Solution 'dyndllclass' (2 projects)***///
///***dynapp****(1st project)***//
//----dynapp.cpp----/
#include <windows.h

#include "dynclass.h

int main ( void


PFNCREATEMYCLASS pfnCreateMyClass = NULL
PFNDELETEMYCLASS pfnDeleteMyClass = NULL
PFNGETCLASSMETHOD pfnGetClassMethod = NULL
PMYCLASSMETHOD pDoSomethingUseful = NULL
CMyClass* pMyClass = NULL

HANDLE hDll = INVALID_HANDLE_VALUE

hDll = LoadLibrary ( "dynclass.dll")

if ( INVALID_HANDLE_VALUE == hDll
return ( -1)

pfnCreateMyClass = ( PFNCREATEMYCLASS) GetProcAddress ( hDll,
"CreateMyClass
)

pfnDeleteMyClass = ( PFNDELETEMYCLASS) GetProcAddress ( hDll,
"DeleteMyClass
)

pfnGetClassMethod = ( PFNGETCLASSMETHOD) GetProcAddress ( hDll,
"GetClassMethod
)

if ( pfnDeleteMyClass
&& pfnCreateMyClas
&& pfnGetClassMetho


// et voila - an instance of CMyClass
pMyClass = ( pfnCreateMyClass) ()

pDoSomethingUseful = ( pfnGetClassMethod ())

( pMyClass->*pDoSomethingUseful)()

( pfnDeleteMyClass ( pMyClass))


FreeLibrary ( hDll)

return ( 0)

//----dynclass.h----/
#ifndef __DYNCLASS_
#define __DYNCLASS_

#include <windows.h

#ifdef _DLL // assume this is defined when we build the dl
#define _DYNLINK __declspec( dllexport
#els
#define _DYNLINK __declspec( dllimport
#endi

class _DYNLINK CMyClas
{
public
CMyClass ()

virtual ~CMyClass()

void DoSomethingUseful()
}

typedef void ( CMyClass::*PMYCLASSMETHOD)()

#ifndef _DL
typedef CMyClass* ( *PFNCREATEMYCLASS)()
#els
_DYNLINK CMyClass* CreateMyClass()
{
return ( new CMyClass())

#endi

#ifndef _DL
typedef void ( *PFNDELETEMYCLASS)( CMyClass*)
#els
_DYNLINK void DeleteMyClass ( CMyClass* pObj)
{
delete pObj

#endi

#ifndef _DL
typedef PMYCLASSMETHOD ( *PFNGETCLASSMETHOD)()
#els
_DYNLINK PMYCLASSMETHOD GetClassMethod ()
{
return &CMyClass::DoSomethingUseful

#endi


#endif // ndef __DYNCLASS_
///****dynclass****(2nd project)**//
//----dynclass.cpp---/
#include <windows.h
#include "dynclass.h

HINSTANCE g_hinstThisDll

static BOOL g_bCrippledOS = FALSE
static OSVERSIONINFO g_osviVerInfo

extern "C" int APIENTRY DllMain ( HINSTANCE hInstance,
DWORD dwReason,
LPVOID lpReserve


BOOL bRC = TRUE

if ( DLL_PROCESS_ATTACH == dwReason

// Extension DLL one-time initializatio
g_hinstThisDll = hInstance

g_osviVerInfo.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO)

GetVersionEx( &g_osviVerInfo)

if ( VER_PLATFORM_WIN32_WINDOWS == g_osviVerInfo.dwPlatformId
g_bCrippledOS = TRUE



return ( TRUE)


CMyClass::CMyClass (

OutputDebugString ( "This is CMyClass::CMyClass()\n")


CMyClass::~CMyClass(

OutputDebugString ( "This is CMyClass::~CMyClass()\n")


void CMyClass::DoSomethingUseful()

OutputDebugString ( "This is CMyClass::DoSomethingUseful()\n")

//---dynclass.def---/
LIBRARY dynclass.dl
EXPORT
CreateMyClass @2 PRIVATE ; object creatio
DeleteMyClass @3 PRIVATE ; object destructio
GetClassMethod @4 PRIVATE ; method acces

//----dynclass.h---/
#ifndef __DYNCLASS_
#define __DYNCLASS_

#include <windows.h

#ifdef _DLL // assume this is defined when we build the dl
#define _DYNLINK __declspec( dllexport
#els
#define _DYNLINK __declspec( dllimport
#endi

class _DYNLINK CMyClas
{
public
CMyClass ()

virtual ~CMyClass()

void DoSomethingUseful()
}

typedef void ( CMyClass::*PMYCLASSMETHOD)()

#ifndef _DL
typedef CMyClass* ( *PFNCREATEMYCLASS)()
#els
_DYNLINK CMyClass* CreateMyClass()
{
return ( new CMyClass())

#endi

#ifndef _DL
typedef void ( *PFNDELETEMYCLASS)( CMyClass*)
#els
_DYNLINK void DeleteMyClass ( CMyClass* pObj)
{
delete pObj

#endi

#ifndef _DLL
typedef PMYCLASSMETHOD ( *PFNGETCLASSMETHOD)();
#else
_DYNLINK PMYCLASSMETHOD GetClassMethod ()
{
return &CMyClass::DoSomethingUseful;
}
#endif



#endif // ndef __DYNCLASS_H
/////////////////////////////////////////////////////
When I did 'Build' on this Solution, I got the following:
c:\Kraus-DynDLL\Kraus\dynapp.cpp(23): error C2664: 'GetProcAddress' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
c:\Kraus-DynDLL\Kraus\dynapp.cpp(27): error C2664: 'GetProcAddress' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
c:\Kraus-DynDLL\Kraus\dynapp.cpp(31): error C2664: 'GetProcAddress' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
c:\Kraus-DynDLL\Kraus\dynapp.cpp(48): error C2664: 'FreeLibrary' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
////////////////////////////////////////////////////////////////
I looked it up in MSDN and I found that Compiler Error C2664: 'function': cannot convert parameter number from
'type1' to 'type2'. I do not know how to correct this problem. Please help and tell me how to solve this problem
in my VC++ .NET 2002 (VC++ 7.0).
Thanks in advance,
Scott Chang
 
J

Jeff Partch [MVP]

Scott Chang said:
HANDLE hDll = INVALID_HANDLE_VALUE;

hDll = LoadLibrary ( "dynclass.dll");

if ( INVALID_HANDLE_VALUE == hDll)
return ( -1);

pfnCreateMyClass = ( PFNCREATEMYCLASS) GetProcAddress ( hDll,
"CreateMyClass"
When I did 'Build' on this Solution, I got the following:
c:\Kraus-DynDLL\Kraus\dynapp.cpp(23): error C2664: 'GetProcAddress' :
cannot convert parameter 1 from 'HANDLE' to 'HMODULE'

Hmm...Try it like this...

HMODULE hDll = (HMODULE)INVALID_HANDLE_VALUE;
 
T

Tomas Restrepo \(MVP\)

Hi Scott, Jeff,
cannot convert parameter 1 from 'HANDLE' to 'HMODULE'

Hmm...Try it like this...

HMODULE hDll = (HMODULE)INVALID_HANDLE_VALUE;

Since LoadLibrary() returns NULL on failure, and not INVALID_HANDLE_VALUE, a
wiser choice would be to do this, instead:
HMODULE hDll =NULL;
hDll = LoadLibrary ( "dynclass.dll");

if ( NULL == hDll)
return ( -1);
 
J

Jeff Partch [MVP]

Tomas Restrepo (MVP) said:
Since LoadLibrary() returns NULL on failure, and not INVALID_HANDLE_VALUE, a
wiser choice would be to do this, instead:
HMODULE hDll =NULL;
hDll = LoadLibrary ( "dynclass.dll");

if ( NULL == hDll)
return ( -1);

Yep! :) Thanks, Tomas.
 
G

Guest

Hi Jeff and Thomas
The statment of HMODULE hDLL = NULL; works. Thank you both
Scott Chan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top