473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

function pointer problem in COM

Hi,
I have a com dll and I am using LoadLibrary function for
loading this dll.

After that I want call a createInstance() function.
To call this function I am using a function ptr that got initialized from GetProcAddress() API.

Here I am facing a problem how to initialize this function pointer for CreateInstance()

All i am doing is.
dll name => MyDll
class name =>MyClass

interface name derived from IDispatch(com interface) ==> IMyClassPtr

long (IMyClassPtr::*CreateInstancePtr)(LPCWSTR *name) = NULL;

HMODULE handleMyDll ;
handleMyDll = LoadLibrary(L"MyDll.dll");

CreateInstancePtr = GetProcAddress( handleMyDll,"MyClass::CreateInstance");

if(CreateInstancePtr == NULL)

-when we call GetProcAddress() function it is giving me an error saying cannot convert.

Can any one tell me the syntax for function pointer or
is any thing wrong in GetProcAddress()

Thanks in Advance
Jul 13 '11 #1
5 2532
weaknessforcats
9,208 Expert Mod 8TB
Are you using C++? I think so because of this:

Expand|Select|Wrap|Line Numbers
  1. ..."MyClass::CreateInstance");
When you use GetProcAddress to get a pointer to "MyFunction", there had better be a function named MyFunction in the library that you loaded.

Unfortunately, to support function overloading, your c++ compiler has mangled MyFunction into some unique id like @FF47skidoo. So that's what's in your library but you are asking for "MyFunction".

You must, without fail, in the library code only, define MyFunction as extern "C". That will tell the C++ compiler to not mangle the name "MyFunction". Now GetProcAddress will work.

However, now you have lost function overloading for MyFunction. Remember, a dll and GetProcAdress are old C structures so they have to be used as C.

My advice is to write interface functions in the DLL coded as extern "C" and have those functions call the real C++ functions. Then you export the interface functions so you can use them with GetProcAddress.
Jul 14 '11 #2
thx for replay let me check it out.
Just wanted to know is it right way of using LoadLibrary function to load a COM dll.
Jul 15 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
Yes you can useLoadLibrary to load any dll. A COM dll is still just a dll. As a fine point I would not hard-code the name of the dll. Get the name dynamically at run-time. What you have is OK for test but not for an actual release.

Usually, you use LoadLibrary when you want to control loading and access to the dll. That is, maybe you want to load the dll and call GetProcAddress on all the functions rght at the start.

Otherwise, you can link the .lib version of the dll at build time. The function in the lib is a dummy function. It calls LoadLibrary and then calls GetProcAddress s you don't have to.
Jul 15 '11 #4
thanks for detailed information.
In my case LoadLibrary is loading a com dll successfully.
But
the GetProcAddress() function is returning NULL for createinstance().
Can any one point me why is happning or we cannot use GetProcAddress() for com specific createInstance().
Jul 21 '11 #5
weaknessforcats
9,208 Expert Mod 8TB
Did you read my Post #2 above?

If so, have you coded your dll functions as extern "C"?
Jul 22 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
6
by: murgan | last post by:
Hi people, i am new to this group,this is my first query, friends i want to know the difference between "function pointer" and "pointer to a function" in c lang, so friends please send the...
1
by: Lloyd Dupont | last post by:
I have some managed C++ interacting with native DLL, for good integration I'm setting up some function pointer in the native DLL, passing some function pointer from the managed world....
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.