Solved

open dll...

Posted on 2001-07-08
21
375 Views
Last Modified: 2013-12-14
i wrote a dll in VB and i want to open it from C++
(like createobject in VB). How do u do it from C++?
0
Comment
Question by:gshriki
  • 6
  • 4
  • 4
  • +5
21 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6262396
You can use the LoadLibrary() windows api function to open a DLL in C++.  You can then obtain pointers to procedures (and data) inside the DLL using the GetProcAddress() function.

However, DLLs are not necessarily assiciated with objects in any way.   The fact that you are discussing a function called "createobject" seems suspicious.  What is this DLL for?
0
 
LVL 1

Expert Comment

by:guyfromeast
ID: 6262635
use #import

for example to import ADODB use
#import "C:\Program Files\Common Files\System\ado\msado15.dll"

VC++ compiler will produce .tlh file after first compilation, You can see all objectnames/namespaces/methods/properties definitions in .tlh file.

add next strusture to Your code
//****************************************************
struct InitOle{
     InitOle(){::CoInitialize(NULL);}
     ~InitOle(){::CoUninitialize();}
} _init_InitOle_;
//****************************************************

You need it to initialize OLE and get Your classes working properly.

to create object call (like SET x=CreateObject() in VB)
CreateInstance
like
YourObjectPointer.CreateInstance(__uuidof(YourObjectName)))

to release object call (like SET x=Nothing)
Release
like
YourObjectPointer->Release();

LoadLibrary will not work for objects, in general way, it's for functions. It possible to use LoadLibrary for obtaining objects from COM library, but wish not to discuss that for 100p :)

GoodLuck()
0
 

Author Comment

by:gshriki
ID: 6263996
OK guyfromeast, here is my specific question and for more then 100p...

I have DLL written in VB (ActiveX DLL) named hw.dll, that contains Class module names clsHello. in it I have function named World(X as integer) that returns a string depending on the givven parameter.
as output of the compilation, I have LIB and DLL files.

How can I call the World function from C/C++ ?

Thanks();

0
 
LVL 1

Accepted Solution

by:
ccaprar earned 200 total points
ID: 6264982
here is your sample program:

#include <comdef.h>
#include <iostream>

// do this to get a smart pointer of type _clsHelloPtr
#import "../hw/hw.dll" no_namespace

HRESULT callTheDLL(short x);

int main(int argc, char* argv[])
{
     std::cout << "Initializing COM...." << std::endl;

     // initialize COM
     HRESULT hr = CoInitialize(NULL);
     if(FAILED(hr))
     {
          std::cout << "Error initializing COM...." << hr  << std::endl;
          return 1;
     }

     // call the COM dll function
     hr = callTheDLL(5);
     if(FAILED(hr))
     {
          std::cout << "Error calling COM object ...." << hr  << std::endl;
          return 2;
     }
     
     // cleanup
     std::cout << "Cleaning up ..." << std::endl;
     CoUninitialize();
     return 0;
}

HRESULT callTheDLL(short x)
{
     HRESULT hr = S_OK;
     _clsHelloPtr pHello = NULL;

     try
     {
          pHello.CreateInstance(__uuidof(clsHello));
          _bstr_t bstrResponse = pHello->World(&x);

          std::cout << static_cast<const char *>(bstrResponse) << std::endl;
     }
     catch(_com_error &e)
     {
          hr = e.Error();
     }

     return hr;
}

0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6265002
To make the whole thing work, compile the code as a standard console application, and also compile first the VB dll to have it registered. The DLL doesn't have to be in the same place as the resulting TestHW exe, since the COM subsystem will know where it was registered.

You should set the import path in the example to match the place where your VB dll resides.

Regards, Cri
0
 
LVL 30

Expert Comment

by:Axter
ID: 6265222
Hi (ccaprar), welcome to EE.

All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.

If you read the following link, you'll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

Hi (gshriki):

Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6265254
Hi Axter.

I can't see why I can't respond as an answer when it responds exactly to what someone asked. If anyone has a better/more exact sollution, it can be posted afterwards and accepted as an answer, and my answer rejected.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6265320
>>I can't see why I can't respond as an answer when it
>>responds exactly to what someone asked. If anyone
>>has a better/more exact sollution, it can be posted
>>afterwards and accepted as an answer, and my answer
>>rejected.

That is your choice.  There is nothing to stop you from doing this.
However, for the benifit of the questioner, it would be better if the question was left unlock.
This increases the chances that the questioner gets additional information.

Also, many experts post their answer as comments.  If an expert has already posted an answer as a comment, it's not fair to that expert when another expert post their answer as an answer (after the fact).

Also, according to EE policy, your posted answer (as answer) can not include parts of a previously posted comment from another expert.

You are free to decide which method you want to take.
But if you really have the questioner's best interest in mine, then I would say that posting your answer as a comment would be the preferred method.  Especially when other experts have already posted possible answers as comments.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6265577
>> I can't see why I can't respond as an answer when it responds exactly to what
>> someone asked. If anyone has a better/more exact sollution, it can be posted
>> afterwards and accepted as an answer, and my answer rejected.
That is absolutely correct!!!


>> However, for the benifit of the questioner, it would be better if the
>> question was left unlock.This increases the chances that the
>> questioner gets additional information.
That is your opinion.  And it is not well supported by facts as many locked questions get lots of attention.

>>  If an expert has already posted an answer as a comment,
>> it's not fair to that expert when another expert post their answer
>> as an answer (after the fact).
I agree that is not fair.  but it is not against the rules.  I personaly would not lock a question after a correct answer was supplied, but its your choice.
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6267853
Nietod, just a thought here: do you think that the question was given a correct and complete answer before I posted mine? It seems to me that the gshriki was still waiting for an answer to his question. Of course it wouldn't be fair to post an answer to a question previously asked as a comment, but I feel this is not the case.

And Axter, please stop bugging people with comments non related to a question. This is not the first time.
Thanks.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Expert Comment

by:Zulma9999
ID: 6268238
ccaprar,
Axter is just giving you advise on how to post your answer and many other experts (including myself) believe that answer should not be posted as answers.
This is common practice.
Give it a shot, before you knock it.
0
 

Expert Comment

by:MakeItWork614
ID: 6268282
ccaprar,
Locking the question is not cool at all.
It's pretty insulting to the other experts who already posted answers.

This is a quick way to make a whole bunch of enemies.
You don't need that.

Just do what every body else does.  Post comments.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6268616
>> do you think that the question was given a correct and complete answer
>> before I posted mine?
In this case I can't say. This is not my area of expertese.  What matters most is what you thought at the time and what gshriki thinks at this time.

>> Just do what every body else does.  Post comments.
not everybody does.  There are currently 100s of locked questions in this topic area.
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6268652
I am getting tired of all this useless quarrel. I thought this site is for helping users, not for "making enemies" just because I locked a question. Just take a look at this topic. So much useless comments, just because someone likes or not locking a question.

I can't find any rules against locking questions, and a lot of questions ARE locked here, and as long as the user who posted the question CAN reject the proposed answer and other experts CAN post comments that can be accepet as answers, I think all this disscution really makes no sense.

So let's provide usefull answers to questions, not trying to blame one of us for locking a question or not.
0
 

Expert Comment

by:GUNDAFLORIDA
ID: 6271525
>>I am getting tired of all this useless quarrel.
If you're really tired you can just stop.

This can be avoided if you just post comments like everyone else.

So why don't you try just posting comments next time.

You know if the user likes your question he can change it to answer.
Whats wrong with that.
0
 

Expert Comment

by:Zulma9999
ID: 6271548
ccaprar,
>>I thought this site is for helping users,

If you really want to help the user then post comments.  What's so hard about that?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6271575
ccaprar,
>>And Axter, please stop bugging people with comments non
>>related to a question. This is not the first time.

This web site is setup for free exchange of information.
I don't have the right (nor the ability) to stop you from posting answers as answer.
And in that same manner, you don't have the right to tell me to stop posting the courtesy information that I post.

You have the right to say NO, to my recommendations, but you don't have the right to ask me not to post my message.

I try to post my message as polite and as non-offending as possible.  I'm sorry if it has offended you, because that is not my intent.

If you would like to discuss this further, please email me at david@axter.com

Thank you
0
 
LVL 22

Expert Comment

by:nietod
ID: 6271578
>> This can be avoided if you just post comments like everyone else.
Seing an many experts post answers, then the best way to avoid these arguments would be for you to not suggest that any expert not post an answer.

>> If you really want to help the user then post comments
or answers.  

The EE rules specifically allow answers to be posted when they meet specific criteria.  You might not like this, but that is the way EE works.  

If you wish to continue this debate, you should do it in the a lounge question.  This question is for solving gshriki's problem.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6271586
>>This question is for solving gshriki's problem.
I agree.
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6271964
I agree we should stop the comment vs. answer here and try helping gshriki. Anyone asked him if he succeeded doing the job? Or if he needs more help?
0
 

Author Comment

by:gshriki
ID: 6272058
Hey everyone, I'm still checking ccaprar's answer. I will let you know when i;ll have more info.

thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now