Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TOUGH:How to call a C function from Access97?

Posted on 2000-03-04
9
Medium Priority
?
226 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
LVL 11

Accepted Solution

by:
alexo earned 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 8 hours left to enroll

971 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