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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
allymConnect With a Mentor Commented:
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
 
PennywisdomAuthor Commented:
Edited text of question.
0
 
PennywisdomAuthor Commented:
Well allym thanks alot

If you could just tell me how to test it I will give you your points
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.