Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to get at C++ class/methods using GetProcAddress

 
   Soft32 Home -> PDA -> Pocket PC Developer RSS
Next:  CeRunappatevent()  
Author Message
Tim Johnson

External


Since: Nov 03, 2003
Posts: 87



(Msg. 1) Posted: Sun Mar 20, 2005 4:36 pm
Post subject: How to get at C++ class/methods using GetProcAddress
Archived from groups: microsoft>public>pocketpc>developer (more info?)

I've got an evc4.0 C++ program that needs to access functions in a
class-library dll. This dll has several different versions, with differing
numbers of functions in different sequence, so I don't want to rely on its
various .lib files. Instead I want to use LoadLibrary and get the addresses
of the few functions I need that happen to live in all versions of the dll.

However, the functions I need are methods in a class. Normally I'd do this:

myApi = new CTarget;
myApi->fcn1();

But when I use LoadLibrary and then GetProcAddress on just the (decorated
C++) method names I get a DataMisalignment first-chance exception. I'm
thinking it's because I can't really just invoke the class methods directly
like that. I think I need to somehow:

a) call the constructor first as though it's a regular method, and
b) pass the equivalent of the "this" pointer as a first argument to every
method of the class I invoke.

Can anyone shed some light on how to do these things?

--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625
Back to top
Login to vote
Joćo Paulo Figueira [eMVP

External


Since: Apr 15, 2004
Posts: 110



(Msg. 2) Posted: Mon Mar 21, 2005 2:55 am
Post subject: Re: How to get at C++ class/methods using GetProcAddress [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Generally you should do this by calling a function that returns an intance
of your class. This function is a regular C exported function and works like
a class factory of sorts. This way, your app only has to know what function
to get for each DLL and you will have access to your classes.

--
Joćo Paulo Figueira
Embedded MVP
"Tim Johnson" <tjohnson DeleteThis @high-point.com> wrote in message
news:uutUNBeLFHA.2468@tk2msftngp13.phx.gbl...
> I've got an evc4.0 C++ program that needs to access functions in a
> class-library dll. This dll has several different versions, with
> differing numbers of functions in different sequence, so I don't want to
> rely on its various .lib files. Instead I want to use LoadLibrary and get
> the addresses of the few functions I need that happen to live in all
> versions of the dll.
>
> However, the functions I need are methods in a class. Normally I'd do
> this:
>
> myApi = new CTarget;
> myApi->fcn1();
>
> But when I use LoadLibrary and then GetProcAddress on just the (decorated
> C++) method names I get a DataMisalignment first-chance exception. I'm
> thinking it's because I can't really just invoke the class methods
> directly like that. I think I need to somehow:
>
> a) call the constructor first as though it's a regular method, and
> b) pass the equivalent of the "this" pointer as a first argument to every
> method of the class I invoke.
>
> Can anyone shed some light on how to do these things?
>
> --
>
> Tim Johnson
> High Point Software, Inc.
> www.high-point.com
> (503) 312-8625
>
>
>
Back to top
Login to vote
Robert Burdick [eMVP]

External


Since: Oct 29, 2004
Posts: 117



(Msg. 3) Posted: Mon Mar 21, 2005 4:19 pm
Post subject: RE: How to get at C++ class/methods using GetProcAddress [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can __declspec(dllexport) individual C++ methods or entire classes.

"Tim Johnson" wrote:

> I've got an evc4.0 C++ program that needs to access functions in a
> class-library dll. This dll has several different versions, with differing
> numbers of functions in different sequence, so I don't want to rely on its
> various .lib files. Instead I want to use LoadLibrary and get the addresses
> of the few functions I need that happen to live in all versions of the dll.
>
> However, the functions I need are methods in a class. Normally I'd do this:
>
> myApi = new CTarget;
> myApi->fcn1();
>
> But when I use LoadLibrary and then GetProcAddress on just the (decorated
> C++) method names I get a DataMisalignment first-chance exception. I'm
> thinking it's because I can't really just invoke the class methods directly
> like that. I think I need to somehow:
>
> a) call the constructor first as though it's a regular method, and
> b) pass the equivalent of the "this" pointer as a first argument to every
> method of the class I invoke.
>
> Can anyone shed some light on how to do these things?
>
> --
>
> Tim Johnson
> High Point Software, Inc.
> www.high-point.com
> (503) 312-8625
>
>
>
>
Back to top
Login to vote
Tim Johnson

External


Since: Nov 03, 2003
Posts: 87



(Msg. 4) Posted: Tue Mar 22, 2005 2:56 am
Post subject: Re: How to get at C++ class/methods using GetProcAddress [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sorry for being so dense, I guess I'm not understanding that. Maybe I just
am unclear on what you mean by "my class".

Here's a little more detail. There is a target dll that exports a class and
all its methods as CTarget. I don't want to link with the .lib file for
this dll, since each version requires a different .lib (the authors of the
dll add or alter sequence of the methods inside the dll with each new
version). So I would like to use LoadLibrary and GetProcAddress to get at
the few functions I really need.

Then I have my own class defined as CProxy. It has a placeholder method for
every method in the CTarget class. In my constructor I grab an instance of
the target class, though I'm not sure how to use it after that. Then my app
calls my LoadLib method to get the function addresses. So for example the
"fcn1" method I define in my class as:

CTarget *m_pTarget;
HMODULE m_lib;
int (*m_pFcn1) (int test);

CProxy::CProxy()
{
m_pTarget = new CProxy();
}

CProxy::LoadLib()
{
m_lib = LoadLibrary(_T("target.dll"));
m_pFcn1 = GetProcAddress(m_lib, _T("fcn1")); //I actually use a
decorated name for fcn1 from dumpbin
}

int CProxy::fcn1(int test)
{
return (*m_pFcn1) (test);

//Also tried these which fail:
return m_pFcn1(test);
return m_pFcn1(m_pTarget, test); // doesn't compile - wrong num args
}


--

Tim Johnson
High Point Software, Inc.
www.high-point.com
(503) 312-8625


"Joćo Paulo Figueira [eMVP]" <joao.figueira.RemoveThis@primeworks.takethisout.pt> wrote
in message news:%23musePfLFHA.1884@TK2MSFTNGP15.phx.gbl...
> Generally you should do this by calling a function that returns an intance
> of your class. This function is a regular C exported function and works
> like a class factory of sorts. This way, your app only has to know what
> function to get for each DLL and you will have access to your classes.
>
> --
> Joćo Paulo Figueira
> Embedded MVP
> "Tim Johnson" <tjohnson.RemoveThis@high-point.com> wrote in message
> news:uutUNBeLFHA.2468@tk2msftngp13.phx.gbl...
>> I've got an evc4.0 C++ program that needs to access functions in a
>> class-library dll. This dll has several different versions, with
>> differing numbers of functions in different sequence, so I don't want to
>> rely on its various .lib files. Instead I want to use LoadLibrary and
>> get the addresses of the few functions I need that happen to live in all
>> versions of the dll.
>>
>> However, the functions I need are methods in a class. Normally I'd do
>> this:
>>
>> myApi = new CTarget;
>> myApi->fcn1();
>>
>> But when I use LoadLibrary and then GetProcAddress on just the (decorated
>> C++) method names I get a DataMisalignment first-chance exception. I'm
>> thinking it's because I can't really just invoke the class methods
>> directly like that. I think I need to somehow:
>>
>> a) call the constructor first as though it's a regular method, and
>> b) pass the equivalent of the "this" pointer as a first argument to every
>> method of the class I invoke.
>>
>> Can anyone shed some light on how to do these things?
>>
>> --
>>
>> Tim Johnson
>> High Point Software, Inc.
>> www.high-point.com
>> (503) 312-8625
>>
>>
>>
>
>
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Dialog class - Hi, I want to do a dialog class based in CDialog class, for using it in a Dialog based project. Is it possible? becaus...

Help with class returning Object - I'm trying to setup a Class that has the ability to use the System.Drawing.Graphics.Drawstring I want to pass this som...

Bug in destructor of CCeSocket class - Hi! I have an iPAQ 3600 with PocketPC 2000, and I want to write a server application that accepts incoming connectio...

CoCreateInstance Class not registered error even though it.. - Hi, I am porting an application from PocketPC 2002 to PocketPC 2003 using evc 4.0. The code worked perfectly in Poc...

Urgent Help! Class disappeared from ClassView - While editing my project, a class just disappeared from the ClassView page but its cpp and h files are still seen in th...

Can you use the Trace class in the compact framework? - If so, how do you create a TraceListener and add on to the TraceListenerCollection? (Also, I notice that Trace only ex...
       Soft32 Home -> PDA -> Pocket PC Developer All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
 Mac
  PDA


[ Contact us | Terms of Service/Privacy Policy ]