473,657 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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****(1 st project)***//
//----dynapp.cpp----/
#include <windows.h

#include "dynclass.h

int main ( void
PFNCREATEMYCLAS S pfnCreateMyClas s = NULL
PFNDELETEMYCLAS S pfnDeleteMyClas s = NULL
PFNGETCLASSMETH OD pfnGetClassMeth od = NULL
PMYCLASSMETHOD pDoSomethingUse ful = NULL
CMyClass* pMyClass = NULL

HANDLE hDll = INVALID_HANDLE_ VALUE

hDll = LoadLibrary ( "dynclass.d ll")

if ( INVALID_HANDLE_ VALUE == hDll
return ( -1)

pfnCreateMyClas s = ( PFNCREATEMYCLAS S) GetProcAddress ( hDll,
"CreateMyCl ass
)

pfnDeleteMyClas s = ( PFNDELETEMYCLAS S) GetProcAddress ( hDll,
"DeleteMyCl ass
)

pfnGetClassMeth od = ( PFNGETCLASSMETH OD) GetProcAddress ( hDll,
"GetClassMe thod
)

if ( pfnDeleteMyClas s
&& pfnCreateMyClas
&& pfnGetClassMeth o
// et voila - an instance of CMyClass
pMyClass = ( pfnCreateMyClas s) ()

pDoSomethingUse ful = ( pfnGetClassMeth od ())

( pMyClass->*pDoSomethingU seful)()

( pfnDeleteMyClas s ( 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 DoSomethingUsef ul()
}

typedef void ( CMyClass::*PMYC LASSMETHOD)()

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

#endi

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

#endi

#ifndef _DL
typedef PMYCLASSMETHOD ( *PFNGETCLASSMET HOD)()
#els
_DYNLINK PMYCLASSMETHOD GetClassMethod ()
{
return &CMyClass::DoSo methingUseful

#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_ATT ACH == dwReason

// Extension DLL one-time initializatio
g_hinstThisDll = hInstance

g_osviVerInfo.d wOSVersionInfoS ize = sizeof ( OSVERSIONINFO)

GetVersionEx( &g_osviVerIn fo)

if ( VER_PLATFORM_WI N32_WINDOWS == g_osviVerInfo.d wPlatformId
g_bCrippledOS = TRUE

return ( TRUE)
CMyClass::CMyCl ass (

OutputDebugStri ng ( "This is CMyClass::CMyCl ass()\n")
CMyClass::~CMyC lass(

OutputDebugStri ng ( "This is CMyClass::~CMyC lass()\n")
void CMyClass::DoSom ethingUseful()

OutputDebugStri ng ( "This is CMyClass::DoSom ethingUseful()\ 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 DoSomethingUsef ul()
}

typedef void ( CMyClass::*PMYC LASSMETHOD)()

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

#endi

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

#endi

#ifndef _DLL
typedef PMYCLASSMETHOD ( *PFNGETCLASSMET HOD)();
#else
_DYNLINK PMYCLASSMETHOD GetClassMethod ()
{
return &CMyClass::DoSo methingUseful;
}
#endif

#endif // ndef __DYNCLASS_H
/////////////////////////////////////////////////////
When I did 'Build' on this Solution, I got the following:
c:\Kraus-DynDLL\Kraus\dy napp.cpp(23): error C2664: 'GetProcAddress ' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
c:\Kraus-DynDLL\Kraus\dy napp.cpp(27): error C2664: 'GetProcAddress ' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
c:\Kraus-DynDLL\Kraus\dy napp.cpp(31): error C2664: 'GetProcAddress ' : cannot convert parameter 1 from 'HANDLE' to 'HMODULE'
c:\Kraus-DynDLL\Kraus\dy napp.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
Nov 17 '05 #1
4 5710
"Scott Chang" <an*******@disc ussions.microso ft.com> wrote in message
news:16******** *************** ***********@mic rosoft.com...
HANDLE hDll = INVALID_HANDLE_ VALUE;

hDll = LoadLibrary ( "dynclass.dll") ;

if ( INVALID_HANDLE_ VALUE == hDll)
return ( -1);

pfnCreateMyClas s = ( PFNCREATEMYCLAS S) GetProcAddress ( hDll,
"CreateMyCl ass"
When I did 'Build' on this Solution, I got the following:
c:\Kraus-DynDLL\Kraus\dy napp.cpp(23): error C2664: 'GetProcAddress ' :

cannot convert parameter 1 from 'HANDLE' to 'HMODULE'

Hmm...Try it like this...

HMODULE hDll = (HMODULE)INVALI D_HANDLE_VALUE;

--
Jeff Partch [VC++ MVP]
Nov 17 '05 #2
Hi Scott, Jeff,
HANDLE hDll = INVALID_HANDLE_ VALUE;

hDll = LoadLibrary ( "dynclass.dll") ;

if ( INVALID_HANDLE_ VALUE == hDll)
return ( -1);

pfnCreateMyClas s = ( PFNCREATEMYCLAS S) GetProcAddress ( hDll,
"CreateMyCl ass"
When I did 'Build' on this Solution, I got the following:
c:\Kraus-DynDLL\Kraus\dy napp.cpp(23): error C2664: 'GetProcAddress ' :

cannot convert parameter 1 from 'HANDLE' to 'HMODULE'

Hmm...Try it like this...

HMODULE hDll = (HMODULE)INVALI D_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);

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #3
"Tomas Restrepo (MVP)" <to****@mvps.or g> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
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.

--
Jeff...
Nov 17 '05 #4
Hi Jeff and Thomas
The statment of HMODULE hDLL = NULL; works. Thank you both
Scott Chan

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
25639
by: David Hoffman | last post by:
When I compile it, I get a "error C2664: 'ExtractID' : cannot convert parameter 1 from 'char' to 'char '" error and I don't understand why. I am just learning C++, so keep it simple. One more note, I have the program compiled using strings instead of char; however, I should be able to convert all my strings to char and get the program to compile. Right? Here is my code. Thanks. // Written by David Hoffman // Sept 30, 2003
3
2985
by: Eamonn O'Connell | last post by:
Hi, We have an ASP.net application, where we dynamically add controls, in the Page_Load. Occasionally, when the user posts back the page (e.g. clicks Save button on the page), we get a "Failed to load viewstate" error. This happens on Windows Server 2003. It happens erratically, and appears to happen after the application has been running (and in use) for some time. When the problem arises, it appears to happen more frequently for a...
1
2549
by: mkarja | last post by:
Hi, I've been struggling with this error for a while and I don't know what's wrong. Any help would be appreciated. Here's code snippets and the error message. ---------------------------- LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, LPARAM lParamClient, HWND _hClient); ----------------------------
4
4146
by: Menny | last post by:
I'm moving from VC6 to VC2003 and from multi-byte to Unicode. After fixing all the errors related to unicode, i ended with an error i can not resolve. ************ error C2664: 'InterlockedExchangePointer' : cannot convert parameter 1 from 'T ** ' to 'void ** ' with Types pointed to are unrelated; conversion requires
1
1733
by: Nancy Sui | last post by:
I am trying to dynamically load a dll which specifies different rules for evalution with the following code. DataRow dr = ds.Tables.Rows; string fileName = Convert.ToString(dr); assemblyInstance = Assembly.Load( fileName ); typeInstance = assemblyInstance.GetType( "MSF.Storefront.Rules." + dr.ToString() , true, false); Object parameter = new Object; if (seg == null) parameter = cs;
3
16807
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
13
8610
by: Challenger | last post by:
Hi I am creating a program in C++ using visual studios 2003, and for a project I am required to create a application. In this application for testing purposes I am outputing a number to show that a certain key was pressed. I am getting error C2664: 'TextOutA' : cannot convert parameter 4 from 'std::string' to 'LPCSTR' As the error. My code is /* Trim fat from windows*/ #define WIN32_LEAN_AND_MEAN
5
1971
by: slippingslider | last post by:
I'm trying to learn how to set ranges by using classes and I get Error c2664 and C2228 when I build. I saw the question in my practice texrbook and I cant figure out what I'm missing. It says "ui" should have a class on the left of "ui' but it already does. Its declared right after the Range. Can you help #include <iostream> #include <iomanip> #include <string> #include <cctype>
1
3425
by: td0g03 | last post by:
I have no idea why am I getting an error C2664: 'parseInput' : cannot convert parameter 2 from 'main::WORD_STRUCT ' to 'struct WORD_STRUCT ' #include <stdio.h> #include <stdlib.h> #include <string.h> #define FILENAME 80 #define MAX_SIZE 1000 #define CHAR_SIZE 200
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.