How to call a function from dll?

Nov 29, 2011 at 8:23am
Hello all,


how would i use this function in visual c++ 6 from a dll:

function LoadRoentecSpectrum(FileName:PChar;SpectrumBuffer:Pointer;var SpectrumBufferSize:LongInt):LongInt;stdcall external
'rtspectrumlib.dll';

???



Any help appreciated!
Nov 29, 2011 at 10:23am

For this try:

1
2
3
HINSTANCE LoadMe = LoadLibrary("C:/Program Files (x86)/Gatan/DMSDK/TemplatePlugIn/RTSpectrumLib.dll");
typedef long ( LoadRoentecSpectrumDatabasefuncPtr)(char * argv );  
LoadRoentecSpectrumDatabasefuncPtr LoadRoentecSpectrumDatabaseEntryPoint = reinterpret_cast <long>(GetProcAddress(LoadMe,"LoadRoentecSpectrumDatabase"));


IT gives me error C2072:

error C2072: 'LoadRoentecSpectrumDatabaseEntryPoint' : initialization of a function


???
Nov 29, 2011 at 10:57am

For this try

extern "C" __declspec(dllimport)long LoadRoentecSpectrumDatabase(char * argv);

It gives me error:

C:\Program Files (x86)\Gatan\DMSDK\TemplatePlugIn\hello.cpp(49) : error C2664: 'LoadRoentecSpectrumDatabase' : cannot convert parameter 1 from 'void *' to 'char *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast


???
Nov 29, 2011 at 11:01am

the function from the dll looks like:


// Load Roentec spectrum database from file
function LoadRoentecSpectrumDatabase(FileName:PChar):LongInt;stdcall external 'rtspectrumlib.dll';

And in Delphi XE2 it is used like:
res,i : longint;
res:=LoadRoentecSpectrumDatabase(PChar(LoadDatabaseDialog.FileName));


HOwever i am using it in visual c++6...,

PChar pointer to zero terminated character array
Nov 29, 2011 at 11:44am
how does the line at 'hello.cpp(49)' look like?
Nov 30, 2011 at 9:40am
Hello coder777 and thanks for the tip.
I got a little further, now to

1
2
3
4
5
6
extern "C" __declspec(dllimport)long LoadRoentecSpectrumDatabase(char * argv);

long res;
char * pathname;
bool da =  DM::OpenDialog( pathname );
res = LoadRoentecSpectrumDatabase(pathname);


it says:

hello.cpp
C:\Program Files (x86)\Gatan\DMSDK\TemplatePlugIn\hello.cpp(45) : warning C4700: local variable 'pathname' used without having been initialized
Linking...
   Creating library Debug/TemplatePlugIn.lib and object Debug/TemplatePlugIn.exp
hello.obj : error LNK2001: unresolved external symbol __imp__LoadRoentecSpectrumDatabase
C:\Program Files (x86)\Gatan\DigitalMicrograph\PlugIns/TemplatePlugIn.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.



i don't know what do to...

Does the char need to be a zero terminated character array for this to work? how would i try that?
pathname = pathname + '/0'
does not seem to do anything...
Nov 30, 2011 at 9:59am
Did you link the .lib generated with the DLL?
Nov 30, 2011 at 10:02am

I thought that i was correctly added the dll to the project by using in visual c++ 6
Project->Add To Project -> Files

but when i add the dll to the linker
Project -> Settings -> Object/Library Modules

It says

Linking...
C:\Program Files (x86)\Gatan\DMSDK\TemplatePlugIn\RTSpectrumLib.dll : fatal error LNK1136: invalid or corrupt file
Error executing link.exe.


the dll should be fine. Does it make difference that this dll was compiled with delphi?
Nov 30, 2011 at 10:17am
Don't know the first thing about visual c++ 6 (edit: nor Delphi :) ). What I know is that when you compile a DLL you get:
1. the DLL
2. a .lib file

The .lib file is the one that needs to be specified as an input to the linker, not the DLL. The DLL just has to be in the same folder as the .exe.

You might also want to check that everything's ok with your DLL by inspecting it with DependencyWalker (since it's extern C it will have a human readable name).

Last edited on Nov 30, 2011 at 10:35am
Nov 30, 2011 at 10:30am
I don't have any lib files. I only got the dll and an example code in delphi.
However i have to use visual c++6.

In delphi code it works like this:

1
2
// Load Roentec spectrum database from file
function LoadRoentecSpectrumDatabase(FileName:PChar):LongInt;stdcall external 'rtspectrumlib.dll';
Nov 30, 2011 at 11:01am

This one at least builds, but don't yet know if it actually works:
1
2
3
4
5
6
7
8
9
HINSTANCE LoadMe = LoadLibrary(TEXT("C:/Program Files (x86)/Gatan/DMSDK/TemplatePlugIn/RTSpectrumLib.dll"));
typedef long (__cdecl *MYPROC)(char * arg2); 
MYPROC ProcAdd = (MYPROC) GetProcAddress(LoadMe, "LoadRoentecSpectrumDatabase"); 

long res;
char * pathname;

bool da =  DM::OpenDialog(pathname );
res = ProcAdd(pathname);
Nov 30, 2011 at 3:03pm

Does anybody know how to use Delphi languages "Packed record" in c++?

From the help file i have this function in Delphi:

1
2
3
4
5
// Read information from loaded database
function GetRoentecSpectrumDatabaseInfo(var DatabaseInfo:TRTDatabaseInfo;
PreviewImageBuffer:Pointer;var ImageBufferSize:LongInt;
SpectrumBuffer:Pointer;var SpectrumBufferSize:LongInt):LongInt;stdcall external
'rtspectrumlib.dll'; 


In the example this is used like this:

1
2
3
4
5
6
7
var   Info           : TRTDatabaseInfo;
         Info.Version:=1;
         Info.Size:=SizeOf(Info);
         // Read database info without preview image and spectrum
         ImgBufferSize:=0;
         SpcBufferSize:=0;
         res:=GetRoentecSpectrumDatabaseInfo(Info,nil,ImgBufferSize,nil,SpcBufferSize);


Also from help:
1
2
3
4
5
6
7
8
9
10
// Database info structure
TRTDatabaseInfo   = packed record
Version : LongInt;
Size : LongInt;
ImageWidth        : LongInt;
ImageHeight       : LongInt;
ImageChannels     : LongInt;
ActiveDetectors   : LongInt;
MaxChannelCount   : LongInt;
end;



Has anybody any experience about this?
What is this "nil"?
Topic archived. No new replies allowed.