C++ DLL (Visual Studio) open with PureBasic

Just starting out? Need help? Post your questions and find answers here.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

C++ DLL (Visual Studio) open with PureBasic

Post by cas »

Hi,
i am trying to create dll with C++ and Visual Studio 2010 Professional. This is my simple procedure for testing:

Code: Select all

#include "stdafx.h"

using namespace std;

#define EXPORT extern "C" __declspec(dllexport)

EXPORT int testfunction(const int test) {
  return test * 3;
}
This works fine with PB and CallCFunction(Fast). But, i need to create dll which use CallFunction(Fast) and not CallCFunction(Fast). I'm interested if that is possible.

Thanks
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: C++ DLL (Visual Studio) open with PureBasic

Post by SFSxOI »

You want to call a .dll function with CallFunctionFast() from a .dll? I'm a little unclear on what your trying to do.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: C++ DLL (Visual Studio) open with PureBasic

Post by cas »

I want to create dll with Visual Studio and then use PureBasic to call function from that dll with CallFunction():

Code: Select all

Define dll=OpenLibrary(#PB_Any,"test.dll")
Debug CallFunction(dll,"testfunction",1)
Sorry for my poor English :oops:
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: C++ DLL (Visual Studio) open with PureBasic

Post by Fred »

you need to change your function declaration to be stdcall (_stdcall keyword).
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: C++ DLL (Visual Studio) open with PureBasic

Post by cas »

Thanks for response, Fred. I tried it before but then i must call it with CallFunction(dll,"_testfunction@4",1).

A problem is, i am writing a plugin for application written in PureBasic which has hardcoded function names and uses CallFunction() and now there is prefix '_' and suffix '@4' so it doesn't recognize that dll as its plugin.

The only solution i see now it would be to create lib with Visual Studio and then create dll in PureBasic and use ImportC to create wrapper for that library.

Or is there something else i can do to create dll with function names without '_' and '@' that uses stdcall?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: C++ DLL (Visual Studio) open with PureBasic

Post by SFSxOI »

Code: Select all

LibFunc = LoadLibrary_("my.dll")
  If LibFunc
    *Func = GetProcAddress_(LibFunc, "MyFunction")
    If *Func
    CallFunctionFast(*Func, parameter, parameter, parameter, parameter, etc...) ; Just ever how many parameters are needed for function
    EndIf
  EndIf
  FreeLibrary_(LibFunc)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
klaver
Enthusiast
Enthusiast
Posts: 147
Joined: Wed Jun 28, 2006 6:55 pm
Location: Schröttersburg

Re: C++ DLL (Visual Studio) open with PureBasic

Post by klaver »

cas, open the .dll in a HEX editor and replace "_testfunction@4" with "testfunction".
Image
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: C++ DLL (Visual Studio) open with PureBasic

Post by cas »

@SFSxOI: I only need Visual Studio C++ source code for creating a dll which can be opened in PureBasic with that code that you just posted but currently it needs to be "_MyFunction@4" and i need it to be opened with "MyFunction".

@klaver: 8) I will try that. It could be a little inconvenience but if it works... then great.


Thanks for trying to help me with my issue.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: C++ DLL (Visual Studio) open with PureBasic

Post by SFSxOI »

why don't you just convert the C++ source to PureBasic?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: C++ DLL (Visual Studio) open with PureBasic

Post by cas »

SFSxOI wrote:why don't you just convert the C++ source to PureBasic?
Because:
*not everyone has PureBasic so i want to make simple plugin template for compiling plugin (dll) with C++ compilers, and
*main application has already shipped with hardcoded function names in CallFunction().
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: C++ DLL (Visual Studio) open with PureBasic

Post by cas »

klaver wrote:cas, open the .dll in a HEX editor and replace "_testfunction@4" with "testfunction".
I tried it today and it looks like it works :)

But... i found solution without editing dll with hex editor. In Visual Studio i set custom definition file in: Project Properties -> Linker -> Input -> Module Definition File and now it creates stdcall dll without '_' and '@', just like PureBasic does. :)

Thank you all again for trying to help me
Post Reply