Page 1 of 1

How can I use a PB DLL on Visual Studio?

Posted: Thu Aug 25, 2011 7:30 am
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?

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

Posted: Thu Aug 25, 2011 7:41 am
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:

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

Posted: Thu Aug 25, 2011 7:56 am
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) ...

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

Posted: Thu Aug 25, 2011 1:36 pm
by freepurebasic
"nice" it's a riming with ICE :twisted: uninstall that visual studio , how manny years you want to stay billy's slave

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

Posted: Thu Aug 25, 2011 2:04 pm
by Fred
Freepurebasic, please stop your useless flamewar posts. Thank you.

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

Posted: Thu Aug 25, 2011 4:23 pm
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.

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

Posted: Tue Feb 27, 2018 2:18 pm
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.

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

Posted: Tue Feb 27, 2018 3:44 pm
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.

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

Posted: Tue Feb 27, 2018 4:35 pm
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.