Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't call AddNumbers() in mydll.dll from a C program
#1
I'm a C/C++ programmer and have very little knowledge about FB or BASIC in general, so please bear with me.

In my project, I need to write C/C++ program that'll call routines implemented by someone else in FB, since static libs and dlls can be created using FB. As a beginner trying to take the first step, I created the sample mydll.dll using fbc (fbc -dll mydll.bas) and am now trying to call the AddNumbers sub from my C code in VC++ on a windows system. VC++ cannot resolve AddNumbers even though I'm linking it with the mydll import library. To make sure that its not a naming convention issue, I tried renaming libmydll.dll.a to mydll.lib. VC++ will still not link it, giving me the error "error LNK2001: unresolved external symbol _AddNumbers".

Here's my C program that I'm using to test calling VB Subs from C code:

#include <stdio.h>

extern "C" {

int AddNumbers(int a, int b);

int main(int argc, char* argv[])
{
int a = 5;
int b = 7;
int sum = 0;

sum = AddNumbers(a, b);

printf("AddNumbers returned the sum = %d\n", sum);

return 0;
}

}

I can't figure out what the problem is. Is it that I need to create a header file from the mydll.bi file, but I guess thats what I'm effectively doing by declaring AddNumbers at the beginning of the C prgram. Please help.

thanks

gaurav
Reply
#2
FB (and most basic variant) is a case insensitive unilke c/c++, as you well may know. Unless explicitly told otherwise (with the ALIAS keyword), identifiers are converted to all uppercase when exported to an exe or a dll. Since the .bi file uses alias (you're using the mydll.bas/.bi example, right?), I don't think this is the problem, but you could try calling ADDNUMBERS instead of AddNumbers in your c/c++ file.

I don't know what the default FB calling conversion for dlls is, but to make sure it's C compatible, try adding cdecl in the function declaration (mydll.bi) like this:
declare function AddNumbers lib "mydll" alias "AddNumbers" cdecl ( byval operand1 as integer, byval operand2 as integer ) as integer

Oh, I only see this now, but although I never used dlls in c/c++, I don't think calling it like a static linked library (.a), just declaring the functions prototype and then call it right away, will work. You may need some kind of dll library to fetch the address of the function. In FB that would be (see testload.bas):

Library = DyLibLoad( "mydll" )
AddNumbers = DyLibSymbol( Library, "AddNumbers" )

I hope some of this will get you closer to your goal. Happy programming.
Reply
#3
The first two steps didn't work. Using all caps for AddNumbers didn't help and when I tried adding the 'cdecl', FB compiler turned unfriendly.

However, my C application compiles and links after I added the keyword '_stdcall' while declaring MyNumbers in C code. So the declaration looks like this now:

extern "C" int _stdcall AddNumbers(int a, int b);

The next hurdle is to get it run. It now crashes complaining Access Violation when AddNumbers is called.

gaurav
Reply
#4
maybe you should reask the question at
www.freebasic.net/forum/

you will get a more detailed answears there along with a faster responce.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)