How can I use a PB DLL on Visual Studio?

Just starting out? Need help? Post your questions and find answers here.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

How can I use a PB DLL on Visual Studio?

Post by IceSoft »

Source from the PureBasic DLL:

Code: Select all

ProcedureDLL.i test(a.i, b.i, c.i, d.i) 
  ProcedureReturn (a+b + c+ d)
EndProcedure
Source from the Visual Studio

Code: Select all

extern "C"
{
int test(int,int,int,int);
}

int main()
{
	test (640,480,0,3);
}
I created a shared Dll (PureBasic and got the test.dll, test,lib and test.exp
I add the test.lib to the Additional dependencies on VS2010
Start the VS compiler and got this error:
1>------ Build started: Project: test_Cpp, Configuration: Debug Win32 ------
1>main.obj : error LNK2019: unresolved external symbol _test referenced in function _main
1>H:\PureBasic_Auf_H\__test__\test_lib\test_Cpp\Debug\test_Cpp.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is wrong?
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
freepurebasic
Enthusiast
Enthusiast
Posts: 123
Joined: Fri Sep 24, 2010 12:02 pm
Location: world wide web

Re: How can I use a PB DLL on Visual Studio?

Post by freepurebasic »

if you need that dll then you must write additional code in purebasic to access it.simple!
my opinion is : delete that Visual Studio because it's a joke :twisted:
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: How can I use a PB DLL on Visual Studio?

Post by IceSoft »

I change the extern line and it compiles now. I got an Executable.

Code: Select all

extern "C"  int __stdcall test(int ,int,int,int);
Starting this Executable raised an error ...unable to start correctly (0xc000003b) ...
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
freepurebasic
Enthusiast
Enthusiast
Posts: 123
Joined: Fri Sep 24, 2010 12:02 pm
Location: world wide web

Re: How can I use a PB DLL on Visual Studio?

Post by freepurebasic »

"nice" it's a riming with ICE :twisted: uninstall that visual studio , how manny years you want to stay billy's slave
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How can I use a PB DLL on Visual Studio?

Post by Fred »

Freepurebasic, please stop your useless flamewar posts. Thank you.
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: How can I use a PB DLL on Visual Studio?

Post by buddymatkona »

I have not used a PB .LIB with Visual Studio however Microsoft C++ expects CDecl format. Try ProcedureCDLL instead of ProcedureDLL in your PB code.
smishra
User
User
Posts: 70
Joined: Tue Jun 06, 2006 3:39 pm
Location: Maryland US

Re: How can I use a PB DLL on Visual Studio?

Post by smishra »

This is an old thread, however I recently ran into the same issue. To help other users I am posting the method that worked for me. The whole process was quite fast once I figured out what to do.

I tried using the lib file generated by PB with Visual Studio 2013 and that did not work. I got an unresolved external function even when the linker used the lib file generated by PB. This may be because lib file formats change between different MSVC versions.

I then used Nirsoft's DLL export viewer to view functions exported by the DLL and to ensure that I had not messed anything up. The DLL export viewer gave me the exported function names and their ordinal values (read the Microsoft link further down to figure out the meaning of ordinal numbers)

http://www.nirsoft.net/utils/dll_export_viewer.html

I then created a def file manually using information contained here

https://msdn.microsoft.com/en-us/library/d91k01sh.aspx

The def file is fairly simple and looks like

Code: Select all

LIBRARY   BTREE  
EXPORTS  
   Insert   @1  
   Delete   @2  
   Member   @3  
   Min   @4 
Then using the lib tool available in VS 2013 developer prompt

Code: Select all

lib /machine:i386 /def:testdll.def
I created the lib file.

This worked with VS2013 and I was able to successfully use code written in PB and exported as a dll from C.

I used

Code: Select all

ProcedureCDLL 
to declare exported functions in my PB DLL file. I declared them as

Code: Select all

 extern returntype functionname
in C code.
fabulouspaul
User
User
Posts: 34
Joined: Sun Nov 23, 2014 1:18 pm

Re: How can I use a PB DLL on Visual Studio?

Post by fabulouspaul »

Did you try the example code from the PB help (http://www.purebasic.com/documentation/ ... le.pb.html)?

There are certain windows specific procedures you have to include when compiling a DLL.
smishra
User
User
Posts: 70
Joined: Tue Jun 06, 2006 3:39 pm
Location: Maryland US

Re: How can I use a PB DLL on Visual Studio?

Post by smishra »

Yeah. I had AttachProcess, DetachProcess, AttachThread, DetachThread functions in my code.

For me it only worked when I manually created a def file and then generated a lib file using MSVC lib tool.

If the format of lib file is indeed the case, then maybe PB can generate a def file when a dll is compiled and programmers who want to go the lib file route can generate the lib file using the C compiler tools.

I could also have loaded the dll dynamically and got a function pointer but find the lib method easier.
Post Reply