DLLs

I want to know how to write a simple dll in Visual C++ and Call it in VB.

For example the function :
void WriteToFile()
{
      FILE *fp;
      fp = fopen("c:\\test.txt","w");
      fprintf(fp,"Ca marche");
      fclose(fp);
}

I could I put that in a DLL
PennywisdomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PennywisdomAuthor Commented:
Edited text of question.
0
allymCommented:
Well, in the C++ area, it is _so_ simple - if you've got hte right tool.

If you've got Visual C++ 4.0 or above (possibly even earlier version), you can set up a project using a wizard.  In Version 6, it's File -> New, then select the Projects tab.  Select Win32 DLL, and press OK.

Then you get a wizard with three options, Empty, Simple and DLL that exports symbols.  Choose the third one.  

The wizard creates a DLL with an exported fn.  Just change the fn (theirs just returns an INT) to do whatever you want.  You can even change the name of the fn - just keep the SIMPLEDLL_API at the start of each exported fn.  
So your
SIMPLEDLL_API int fnSimpleDLL(void);
could become
SIMPLEDLL_API int ProcessTheData(int type, int value);

If you want to read two ints and return some value.

If you want to modify the parameters, you need to pass by pointer,
SIMPLEDLL_API int ProcessTheData(int *type, int* value);

Remenber to keep the header Xxx.h in sync with the cpp Xxx.cpp otherwise things could go wrong.

As to the VB side, I don't know.  I don't do VB.  But it's a very common and easy thing to do.  See knowledgebase article Q106553 on the microsoft web site (this article is for V4 I think)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PennywisdomAuthor Commented:
Well allym thanks alot

If you could just tell me how to test it I will give you your points
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

nietodCommented:
>> As to the VB side, I don't know.  I don't do VB
Don't you think that could be a problem?

Penywisdom, you are going to have problems with name decoration (name mangeling).   I've got some info on this somewhere.
0
nietodCommented:
C++ "decorates" or "mangles" function names by appending a "code" to the end of the name that expresses the parameters passed to the function.  This is used to impliment function overloading, where 2 or more distinct functions have the same name and different parameters.  (Apparently they have the same name--C++ mangles the names so they don't actually have the same name).  

Ordinarily the C++ function is exported with the mangeled name.  VB however doesn't do any name mangeling.  So when the VB program tries to import the function it looks for the unmangled name.  So VB cannot find the function written in C++.  

To fix this problem, you declare the function as "extern "C" ".  This dissables the C++ name mangeling on the function.

Now to make things worse, there is another type of name decoration that still goes on.  This is the standard call name decoration.  This decoration appends a "@" followed by a nunmer to the end of each procedure name.  (FYI the number is the size of the procedure's parameters expressed in bytes.)  This form of decoration will still go on.  You can either leave this decoration on the name and specify the decoration in VB, or you can remove the decoration using a module definition file (.DEF.).

Let me know if you have questions.
0
PennywisdomAuthor Commented:
To niedtod,

How do I declare the function as "extern "C" " ?
0
nietodCommented:
just add "extern "C" " to the declaration, like

extern "C" int AddOne(int i)
{
   return i + i;
}
0
PennywisdomAuthor Commented:
Thanks alot allym I'm very greatfull for your help

nietod :
I will create another question called Poinst to nietod worth 50 points
Just anser anything as an anser and I will accept it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.