Real Software Forums
http://forums.realsoftware.com/

Declares: .dlls other than in System32?
http://forums.realsoftware.com/viewtopic.php?f=6&t=40290
Page 1 of 2

Author:  dprowse [ Mon Aug 15, 2011 5:32 pm ]
Post subject:  Declares: .dlls other than in System32?

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);}

Author:  charonn0 [ Mon Aug 15, 2011 9:29 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  dprowse [ Tue Aug 16, 2011 9:37 am ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  charonn0 [ Tue Aug 16, 2011 1:01 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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.

Author:  dprowse [ Tue Aug 16, 2011 2:32 pm ]
Post subject:  Re: Declares: .dlls other than in System32? <closed>

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

Author:  trs [ Wed Aug 17, 2011 8:02 am ]
Post subject:  Re: Declares: .dlls other than in System32?

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;
}

Author:  dprowse [ Wed Aug 17, 2011 9:20 am ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  trs [ Fri Aug 19, 2011 6:47 am ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  dprowse [ Fri Aug 19, 2011 5:02 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

Thanks.
I'll compare in a bit.
D

Author:  simulanics [ Fri Aug 26, 2011 11:44 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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 :-)

Author:  simulanics [ Fri Aug 26, 2011 11:59 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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..

Author:  dprowse [ Sun Sep 04, 2011 4:30 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  dprowse [ Sun Sep 04, 2011 4:40 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  swort [ Tue Sep 06, 2011 8:58 pm ]
Post subject:  Re: Declares: .dlls other than in System32?

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

Author:  rpehlm [ Thu Oct 20, 2011 1:26 am ]
Post subject:  Re: Declares: .dlls other than in System32?

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.

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/