Page 1 of 1

C++ DLL (Visual Studio) open with PureBasic

Posted: Sat May 08, 2010 12:13 am
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

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

Posted: Sat May 08, 2010 12:58 am
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.

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

Posted: Sat May 08, 2010 1:10 am
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:

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

Posted: Sat May 08, 2010 10:50 am
by Fred
you need to change your function declaration to be stdcall (_stdcall keyword).

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

Posted: Sat May 08, 2010 11:14 am
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?

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

Posted: Sat May 08, 2010 2:27 pm
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)

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

Posted: Sat May 08, 2010 3:02 pm
by klaver
cas, open the .dll in a HEX editor and replace "_testfunction@4" with "testfunction".

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

Posted: Sat May 08, 2010 4:21 pm
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.

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

Posted: Sat May 08, 2010 6:18 pm
by SFSxOI
why don't you just convert the C++ source to PureBasic?

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

Posted: Sat May 08, 2010 9:29 pm
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().

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

Posted: Sun May 09, 2010 1:05 pm
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