Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Nov 21, 2019 2:00 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Declares: .dlls other than in System32?
PostPosted: Mon Aug 15, 2011 5:32 pm 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Hi,
Quick question regarding declare and working with 'c' and 'c++' generated dlls.

With the Declare "foo name" "dynLibName.dll" (var of type, etc) as return "type", I can get to the System32 dlls easily. I have my own dll that I would like to access func from but not placed in System32 dir. Is this possilble with RB to redirect?

Further I am having problems with the dll builds. Do I have to extern "C" or extern "C++" all over the place or is __declspec( dllexport ) sufficient? Here is a short dll built to accept 2 short and return a sum short. I can't get attachment with RB. Anyone have this fun before? tia. Derek.

/
/* in header
#define DllExport __declspec( dllexport )
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID);
DllExport short add_num(short, short);

*/
//in main.cpp

#include <windows.h>
//#include <string.h>
#include <ctype.h>
#include "OurDll.h"

BOOL WINAPI DllMain (
HINSTANCE hModule,
DWORD ul_reason_for_call,
LPVOID lpNot)
{
&nbsp; &nbsp;switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
//stuff
case DLL_THREAD_ATTACH:
//stuff
case DLL_THREAD_DETACH:
//stuff
case DLL_PROCESS_DETACH:
//stuff
break;
}

return TRUE;
}

* Add two integers */

DllExport short add_num(short a, short b){
return(a+b);}


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Mon Aug 15, 2011 9:29 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
You can call into a DLL by name if the DLL is located in the system's search path or if the DLL is located in the safe directory as your application. To call a DLL that's in another directory and which is not in the search path you need to give its full path:

Declare Function foo Lib "C:\Program Files\bar\baz.dll" (biff As Integer) As Boolean


You can also check whether a particular function and/or library is availble using the IsFunctionAvailable method:

If System.IsFunctionAvailable("foo", "C:\Program Files\bar\baz.dll") Then
//Function and DLL are present
Else
//Function and DLL are NOT present
End If


Use "soft" declares if there's any real chance that a particular function or library will not be available. This will allow you to gracefully notify the user and exit the app, if necessary:

If System.IsFunctionAvailable("foo", "C:\Program Files\bar\baz.dll") Then
Soft Declare Function foo Lib "C:\Program Files\bar\baz.dll" (biff As Integer) As Boolean
Call foo(0)
Else
Msgbox("The required library function 'foo' was not found on this system!")
Quit(1)
End If

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Tue Aug 16, 2011 9:37 am 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Thanks greatly for the path specifications and the
System.IsFunctionAvailable
functino call; v. nice. I couldn't find this in Lang ref. Just me I suppose.

Just curious though with:
Quote:
Declare Function foo Lib "C:\Program Files\bar\baz.dll" (biff As Integer) As Boolean


it is hard coded. Is there no option for relative paths? If I install to drive that is D:\ as drive letter there will be problem. I'll try this later but do you know if "...\Program Files\bar\baz.dll" would work?

I'll post if so.

Another question for you, as it looks like you have built usable dlls for your programs, what is the best internal structure for dll for use with RB? I have used the declare for use with system dlls only to this point. I have to start playing with my own. Here in lies another learning curve.

I've tried the standard:
#define DllExport __declspec( dllexport )
DllExport short add_num(short, short);


should I be setting calling convention in compilers configuration properties __cdecl or __stdcall? Compiling for 'c' or 'c++' does it matter to RB?

IN RB I get the NilObjectException at run time as the add_num foo is not located, though the dll seems to be as reported by RB.

I know this is not a c/c++ board but I am weak on c/c++ and would just like to get RB working for this small part of the project.

Your help is appreciated.

Derek

RB2007r5 pro
MSVS2005 pro
Win7


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Tue Aug 16, 2011 1:01 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
In my testing a relative path doesn't work. If your app requires a particular library then you should place it next to your app with your installer. That way it will always be on your application's search path.

I've never written my own DLL so I'm afraid I can't really answer your other questions.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32? <closed>
PostPosted: Tue Aug 16, 2011 2:32 pm 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Ok.

The relative path did not work for me either.

I'll stick with app location then for now.

I'll have to open a new topic on the dlls I guess as even with using extern "C" I get nothing acknowledged by RB using MSVS2005 dll wizard. odd.

Thanks for your help though.

Derek


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Wed Aug 17, 2011 8:02 am 
Offline

Joined: Thu May 03, 2007 2:33 pm
Posts: 289
I'm a beginner when it comes to C. However I have had success in building a .dll and calling it from rb.

I am using Dev C++ (an older free IDE I think).

The start of the .c file is:

#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>

#ifdef _WIN32
#include <windows.h>

#define DLL_EXPORT extern "C" __declspec (dllexport)

BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;

case DLL_PROCESS_DETACH:
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;
}

/* Returns TRUE on success, FALSE on failure */
return TRUE;
}

#else
#define DLL_EXPORT
#endif


DLL_EXPORT int Test(int a,int b)
{

return a+b;
}


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Wed Aug 17, 2011 9:20 am 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Thanks trs.

I still can't get a compile.

I don't know why it is so hard to generate a dll that works for rb.

I just used DevC++ to try to compile it off and no luck. I get this:

Quote:
39 C:\Users\..\devcpp\dllCall\dllmain.c syntax error before string constant


If I try to compile off in MS2005 I get a string issue as well. C to C++ incompatability issue I am sure.

Do you know if you changed any Enviro vars to make yours work?

thanks anyway.
Derek


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Fri Aug 19, 2011 6:47 am 
Offline

Joined: Thu May 03, 2007 2:33 pm
Posts: 289
Not sure if this helps but I'll try anyway:

I just checked in my Dev C++ project and its got

Project (menu)->Project Options->Parameters:

Compiler:
-DBUILDING_DLL=1
C++ compiler:
-DBUILDING_DLL=1
Linker:
-no-export-all-symbols -add-stdcall-alias


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Fri Aug 19, 2011 5:02 pm 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Thanks.
I'll compare in a bit.
D


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Fri Aug 26, 2011 11:44 pm 
Offline
User avatar

Joined: Sun Aug 12, 2007 10:10 am
Posts: 1086
Location: Boiling Springs, SC
dprowse wrote:
Thanks trs.

I still can't get a compile.

I don't know why it is so hard to generate a dll that works for rb.

I just used DevC++ to try to compile it off and no luck. I get this:

Quote:
39 C:\Users\..\devcpp\dllCall\dllmain.c syntax error before string constant


If I try to compile off in MS2005 I get a string issue as well. C to C++ incompatability issue I am sure.

Do you know if you changed any Enviro vars to make yours work?

thanks anyway.
Derek



google lcc-win32 its a better "non-basic" compiler :-) the c header files are newer..try looking in your c:\program files\real software\real whatever version\extras\plugin sdk\examples...and try some of that code...there you can create actual plugins and embed them into your application so that no external dll is required :-)

_________________
Matthew A. Combatti
Real Studio 2012 r1.2

Visit Xojo Developer's Spot!
Systems I Use:
Windows XP/Windows Vista/Windows Server 2008 r2/Windows 7/Windows 8
Mac OSX 10.5/Mac OSX 10.6/Mac OSX Server/Ubuntu/Debian/Suse/Red Hat/
Windows Server 2011/CentOS 5.4 /ReactOS/SimOS

~All Xojo Compatible~


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Fri Aug 26, 2011 11:59 pm 
Offline
User avatar

Joined: Sun Aug 12, 2007 10:10 am
Posts: 1086
Location: Boiling Springs, SC
simulanics wrote:
dprowse wrote:
Thanks trs.

I still can't get a compile.

I don't know why it is so hard to generate a dll that works for rb.

I just used DevC++ to try to compile it off and no luck. I get this:

Quote:
39 C:\Users\..\devcpp\dllCall\dllmain.c syntax error before string constant


If I try to compile off in MS2005 I get a string issue as well. C to C++ incompatability issue I am sure.

Do you know if you changed any Enviro vars to make yours work?

thanks anyway.
Derek



google lcc-win32 its a better "non-basic" compiler :-) the c header files are newer..try looking in your c:\program files\real software\real whatever version\extras\plugin sdk\examples...and try some of that code...there you can create actual plugins and embed them into your application so that no external dll is required :-)



or google "BCX basic to c" and download the basic code to C code converter..then write 15 lines of code in a text editor in BASIC (not realbasic) to achieve the same goal...save it as whatever.bas...drop it on the BCX file and wham! it will generate the working valid c code into a file called whatever.c ..just cut and paste the code into your dll file :-)..plugin folder is still your best bet..or write dlls in visual basic...unless you're writing a plugin you really don't need the c code anyway..

_________________
Matthew A. Combatti
Real Studio 2012 r1.2

Visit Xojo Developer's Spot!
Systems I Use:
Windows XP/Windows Vista/Windows Server 2008 r2/Windows 7/Windows 8
Mac OSX 10.5/Mac OSX 10.6/Mac OSX Server/Ubuntu/Debian/Suse/Red Hat/
Windows Server 2011/CentOS 5.4 /ReactOS/SimOS

~All Xojo Compatible~


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Sun Sep 04, 2011 4:30 pm 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Quote:
I'm a beginner when it comes to C. However I have had success in building a .dll and calling it from rb.

I am using Dev C++ (an older free IDE I think).

The start of the .c file is:


Code:

#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>

#ifdef _WIN32
#include <windows.h>
....



Hi trs,

This is the simplest form of dll for export that worked for me (I still have to modify for import on other end...)
I borrowed pretty heavily from the net.

/* myDll.cpp source code */

#include <windows.h>
#include "myDll.h"


//Function declarations
extern "C" __declspec(dllexport) int __cdecl SendIt();


BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpReserved ) // reserved
{
// Perform actions based on the reason for calling.
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;

case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;

case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;

case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE;
}




int SendIt()
//just returns a num for now
{

return 13;
}


the header is:

/*mydll.h*/
#define DllExport __declspec( dllexport ) //macro to make export life easier

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID);
DllExport int sendIt();


Thanks for help.
dp


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Sun Sep 04, 2011 4:40 pm 
Offline

Joined: Wed Sep 26, 2007 11:02 am
Posts: 30
Hi simulanics,

Based on this comment by you:
Quote:
google lcc-win32 its a better "non-basic" compiler the c header files are newer..try looking in your c:\program files\real software\real whatever version\extras\plugin sdk\examples...and try some of that code...there you can create actual plugins and embed them into your application so that no external dll is required


I did a little research and settled on Code::Blocks for all my non msvs required coding. I did look in
Quote:
:\program files\real software\real whatever version\extras\plugin sdk\examples
and now can build my own rbplugins. I'll continue using c/c++ as vb is a little bit on the rusty side for me (VB6 is where I stopped :mrgreen: )

Next to see if I can build a wxWidgets project, wrap that in dll and make a plugin for rb...just for fun.

Thanks for all
dp


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Tue Sep 06, 2011 8:58 pm 
Offline

Joined: Mon May 31, 2010 5:35 am
Posts: 2412
Location: Netherlands
simulanics wrote:
or google "BCX basic to c" and download the basic code to C code converter..then write 15 lines of code in a text editor in BASIC (not realbasic) to achieve the same goal...save it as whatever.bas...drop it on the BCX file and wham! it will generate the working valid c code into a file called whatever.c ..just cut and paste the code into your dll file :-)..plugin folder is still your best bet..or write dlls in visual basic...unless you're writing a plugin you really don't need the c code anyway..


Or just use FreeBasic it can compile Linux Libaries and .dll's on windows..;)
100% free! It's very much like RealBasic and easy once you get it working.. :D

http://www.freebasic.net

_________________
SWORT™
- Windows Vista, Windows XP, Windows 7, Linux Mint 11 and Mac OSX Lion
- REAL Studio Enterprise(latest) and WEB Edition(latest)
- Plugins (MBS Complete, ToringoLib, Einhugur, VanHoek + Some open source plugins)
For Dutch User Groups ask me to enter!


Top
 Profile  
Reply with quote  
 Post subject: Re: Declares: .dlls other than in System32?
PostPosted: Thu Oct 20, 2011 1:26 am 
Offline

Joined: Tue Jun 06, 2006 3:50 pm
Posts: 4
Hi Swort,
Do you have a FreeBasic project to generate a DLL that can be used by RB and that you would be willing to share? Just looking for a simple example to get started. Tried the DLL example in the FreeBasic manual and it does not work in RB2011.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group