Solved

TOUGH:How to call a C function from Access97?

Posted on 2000-03-04
9
211 Views
Last Modified: 2010-04-10
I have to write a C function to be called by Access 97. I can make a Win32 DLL with a function having no argument and it works. But as soon as I add an argument to my function (in C and in Access), it does not work any more: I get  a message from ACCESS saying that the calling convention is incorrect (error 49). I tried with an integer argument in access and short in C; I also tried with a boolean argument in Access and bool in C. PLEASE HELP!
0
Comment
Question by:JeanTrudel
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2583564
You need to declare your function like this:

extern "C" int __declspec(dllexport) __stdcall FunctionName(int);

Cheers,

Raymond.
0
 

Author Comment

by:JeanTrudel
ID: 2583710
I tried it and it did'nt work. Maybe I made an error. Please contact me at jean@trudel.com so that I can send you my small files. I hope I can accept your answer then.
Jean
0
 
LVL 22

Expert Comment

by:nietod
ID: 2583761
rwilson is correct--at least in part

Here is something i wrote for another related question.

**********************

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).  

The function is exported with the mangeled name, so you aren't actually specifying the right name to GetProcAddress().  You can use dumpbin to verify this, you should see the function exported with a mangeled name and that is the name that you woudl have to specify to GetProcAddress()

To fix this problem, you can specify the mangeled name to GetProcAddress(), or--better--declared the function as "extern "C" ".  This dissables name mangeling on the function.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2583772
So the extern "C" will be needed as suggested, if you had problems they may be bacause you made a mistake.  Another posibility is that you may still have the standard call decoration on the name.

If you look at the exports of the DLL using dumpbin you will see the names of the exported procedures and you will see the decoration I mentioned.  (It is very ugly.)  When you use extern "C" the decoration should go away--usually.  However sometimes when functions are declared to use the standard call calling convention the standard call decortion still remains.  This is a "@" followed by a number, like a function called Increment() might actually have a name like Increment@4 in the exports.  You have two choices, you can specify this decoration in the code that uses the function or you can remove this decoration too.  To remove this decoration you need to use a module definiton file.  (.def file) that lists the undecorated name of the function in the exports.


Also note, that you don't have to reject an answer just because it wasn't working for you 100%.  If you think you might have missunderstood or if you just need more help you should try to work with the expert, rather than rejecting the question.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 2583796
I answered similar questions before.  I'll summarizebelow but if you'd like to see a good discussion check:
  http://www.experts-exchange.com/bin/EQ.10205200 (9 points)

There are certain rules that, if followed, will allow your DLL functions to be called from VB code.

1. Only functions with C linkage can be called from VB code.
2. The calling convention must be __stdcall (aka WINAPI).
3. The function name "decoration" must be removed using a DEF file.

In other words, let's say you have a function, MyFunc() that you wish to put into MyCode.DLL and call from VB.

MyCode.cpp will contain the function defined thus:

    extern "C" void WINAPI MyFunc()

MyCode.def will look like:

    LIBRARY MyCode

    EXPORTS
        MyFunc

Note that you MUST use the DEF file and not __declspec(dllexport).

Of course, only global functions may be used (implied by extern "C") and the arguments and return value must be known to VB.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2584040
>> Note that you MUST use the DEF file
I think it depends on the compiler--or something--because in some cases poeple have not needed to do this.
0
 

Author Comment

by:JeanTrudel
ID: 2584326
You and nietod have been of grat help. But your explanation helped me the most. That's why I chose your comment first. I was so happy that many experts were ready to help! That got me out of a big problem: thanks a lot to all of you
0
 
LVL 11

Expert Comment

by:alexo
ID: 2585126
>> You and nietod have been of grat help.

No wonder - I'm actually nietod's alter ego.  In his childhood he used to blame me for his broken toys, etc.  After so many incidents,  I ceased to be fictional and  took a life of my own.  Now I haunt him on EE.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2585150
Fortunately I'm married to a psychologist, so I get help for free.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

20 Experts available now in Live!

Get 1:1 Help Now