Board index » cppbuilder » Read string from DLL

Read string from DLL


2005-06-24 11:32:36 PM
cppbuilder26
I am trying to create a Label on my main form that will look into my DLL
dynamically and pull a constant exported string (which will be the version
number of the DLL) and display it in Label1. Can anyone tell me how to set
it up in the DLL and to call it from the main EXE?
Thanks,
Brad Nelson
 
 

Re:Read string from DLL

Brad Nelson wrote:
Quote
I am trying to create a Label on my main form that will look into my DLL
dynamically and pull a constant exported string (which will be the version
number of the DLL) and display it in Label1. Can anyone tell me how to set
it up in the DLL and to call it from the main EXE?
If you have a DLL then I suppose that you know hou to define your exported
funtions. So just make a function that returns the versionstring as for
example an AnsiString.
In your DLL code:
AnsiString VersionString()
{
return "version 1.2.3.4.6";
}
In your application depending on the way you link that could be as simple as:
Label1->Caption = VersionString();
Hans.
 

Re:Read string from DLL

"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
Quote
I am trying to create a Label on my main form that will look into
my DLL dynamically and pull a constant exported string (which
will be the version number of the DLL) and display it in Label1.
Assuming you are using the DLL's version resources, then use the Win32 API
GetFileVersionInfoSize(), GetFileVersionInfo(), and VerQueryValue()
functions.
Gambit
 

{smallsort}

Re:Read string from DLL

Hans Galema wrote:
Quote
If you have a DLL then I suppose that you know hou to define your exported
funtions. So just make a function that returns the versionstring as for
example an AnsiString.

In your DLL code:

AnsiString VersionString()
{
return "version 1.2.3.4.6";
}
Aren't there some problems/complications with passing AnsiString between
an application and a DLL? It seems I saw that somewhere, but I can't
re-locate it.
-- S.
 

Re:Read string from DLL

"salamander1965" < XXXX@XXXXX.COM >wrote in message
Quote
Aren't there some problems/complications with passing AnsiString
between an application and a DLL?
There can be, if the DLL is not set up properly to use Borland's memory
manager and such.
Gambit
 

Re:Read string from DLL

Does anyone know of some simple code to grab the dll version and display it
in the Label1 using the API calls?
THanks,
Brad
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I am trying to create a Label on my main form that will look into
>my DLL dynamically and pull a constant exported string (which
>will be the version number of the DLL) and display it in Label1.

Assuming you are using the DLL's version resources, then use the Win32 API
GetFileVersionInfoSize(), GetFileVersionInfo(), and VerQueryValue()
functions.


Gambit


 

Re:Read string from DLL

"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
Quote
Does anyone know of some simple code to grab the dll
version and display it in the Label1 using the API calls?
Did you read the API documentation yet?
DWORD dwHandle;
DWORD dwSize = GetFileVersionInfoSize("c:\\path to\\myfile.dll",
&dwHandle);
if( dwSize )
{
LPVOID pVersionInfo = GlobalAlloc(GMEM_FIXED, dwSize);
if( GetFileVersionInfo("c:\\path to\\myfile.dll", dwHandle, dwSize,
pVersionInfo);
{
DWORD dwLen;
char *pBuild;
if( VerQueryValue(pData,
"\\StringFileInfo\\040904E4\\FileVersion", (LPVOID*)&pBuild, &dwLen) )
Label1->Caption = AnsiString(pBuild, dwLen);
}
GlobalFree(pVersionInfo);
}
Gambit
 

Re:Read string from DLL

Thanks for the code! When I compile it says the pData is an undefined
symbol. Any Ideas?
THanks,
Brad
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
news:42bc6f17$ XXXX@XXXXX.COM ...

>Does anyone know of some simple code to grab the dll
>version and display it in the Label1 using the API calls?

Did you read the API documentation yet?

DWORD dwHandle;
DWORD dwSize = GetFileVersionInfoSize("c:\\path to\\myfile.dll",
&dwHandle);
if( dwSize )
{
LPVOID pVersionInfo = GlobalAlloc(GMEM_FIXED, dwSize);
if( GetFileVersionInfo("c:\\path to\\myfile.dll", dwHandle, dwSize,
pVersionInfo);
{
DWORD dwLen;
char *pBuild;

if( VerQueryValue(pData,
"\\StringFileInfo\\040904E4\\FileVersion", (LPVOID*)&pBuild, &dwLen) )
Label1->Caption = AnsiString(pBuild, dwLen);
}
GlobalFree(pVersionInfo);
}


Gambit


 

Re:Read string from DLL

If I replace pData with pVersionInfo it gives me another error: Cannot
convert 'unsigned long *' to unsigned int *'
Any Ideas?
Thanks,
Brad
"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks for the code! When I compile it says the pData is an undefined
symbol. Any Ideas?

THanks,
Brad


"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
news:42bc72b1$ XXXX@XXXXX.COM ...
>
>"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
>news:42bc6f17$ XXXX@XXXXX.COM ...
>
>>Does anyone know of some simple code to grab the dll
>>version and display it in the Label1 using the API calls?
>
>Did you read the API documentation yet?
>
>DWORD dwHandle;
>DWORD dwSize = GetFileVersionInfoSize("c:\\path to\\myfile.dll",
>&dwHandle);
>if( dwSize )
>{
>LPVOID pVersionInfo = GlobalAlloc(GMEM_FIXED, dwSize);
>if( GetFileVersionInfo("c:\\path to\\myfile.dll", dwHandle,
>dwSize,
>pVersionInfo);
>{
>DWORD dwLen;
>char *pBuild;
>
>if( VerQueryValue(pData,
>"\\StringFileInfo\\040904E4\\FileVersion", (LPVOID*)&pBuild, &dwLen) )
>Label1->Caption = AnsiString(pBuild, dwLen);
>}
>GlobalFree(pVersionInfo);
>}
>
>
>Gambit
>
>


 

Re:Read string from DLL

Try this;
Memo4->Clear();
AnsiString VerTemp;
char filename[MAX_PATH];
strcpy (filename, Application->ExeName.c_str());
unsigned long dummy;
int size = GetFileVersionInfoSize (filename, &dummy);
unsigned long *buffer = new unsigned long[size];
GetFileVersionInfo(filename, NULL, size, buffer);
void *lpBuffer;
unsigned int dwBytes;
VerQueryValue(buffer,"\\", &lpBuffer, &dwBytes);
VS_FIXEDFILEINFO *lpvsinfo = (VS_FIXEDFILEINFO *)lpBuffer;
int MajorVersion = HIWORD(lpvsinfo->dwFileVersionMS);
int MinorVersion = LOWORD(lpvsinfo->dwFileVersionMS);
int MajorBuild = HIWORD(lpvsinfo->dwFileVersionLS);
int MinorBuild = LOWORD(lpvsinfo->dwFileVersionLS);
VerTemp = String(MajorVersion) + "."+
String(MinorVersion) + "."+
String(MajorBuild) + "."+
String(MinorBuild);
Memo4->Lines->Add("Program Version: "+VerTemp);
delete []buffer;
It requires a Memo4 on the form, but you can change that to taste.
Regards,
G.
Brad Nelson wrote:
Quote
If I replace pData with pVersionInfo it gives me another error: Cannot
convert 'unsigned long *' to unsigned int *'

Any Ideas?

Thanks,
Brad




"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
news:42bc74e7$ XXXX@XXXXX.COM ...

>Thanks for the code! When I compile it says the pData is an undefined
>symbol. Any Ideas?
>
>THanks,
>Brad
>
>
>"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
>news:42bc72b1$ XXXX@XXXXX.COM ...
>
>>"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
>>news:42bc6f17$ XXXX@XXXXX.COM ...
>>
>>
>>>Does anyone know of some simple code to grab the dll
>>>version and display it in the Label1 using the API calls?
>>
>>Did you read the API documentation yet?
>>
>>DWORD dwHandle;
>>DWORD dwSize = GetFileVersionInfoSize("c:\\path to\\myfile.dll",
>>&dwHandle);
>>if( dwSize )
>>{
>>LPVOID pVersionInfo = GlobalAlloc(GMEM_FIXED, dwSize);
>>if( GetFileVersionInfo("c:\\path to\\myfile.dll", dwHandle,
>>dwSize,
>>pVersionInfo);
>>{
>>DWORD dwLen;
>>char *pBuild;
>>
>>if( VerQueryValue(pData,
>>"\\StringFileInfo\\040904E4\\FileVersion", (LPVOID*)&pBuild, &dwLen) )
>>Label1->Caption = AnsiString(pBuild, dwLen);
>>}
>>GlobalFree(pVersionInfo);
>>}
>>
>>
>>Gambit
>>
>>
>
>


 

Re:Read string from DLL

"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
Quote
When I compile it says the pData is an undefined symbol.
Change this line:
if( VerQueryValue(pData, "\\StringFileInfo\\040904E4\\FileVersion",
(LPVOID*)&pBuild, &dwLen) )
To this instead
if( VerQueryValue(pVersionInfo,
"\\StringFileInfo\\040904E4\\FileVersion", (LPVOID*)&pBuild, &dwLen) )
Gambit
 

Re:Read string from DLL

"Brad Nelson" < XXXX@XXXXX.COM >wrote in message
Quote
If I replace pData with pVersionInfo it gives me another error:
Cannot convert 'unsigned long *' to unsigned int *'
Change this code:
DWORD dwLen;
char *pBuild;
if( VerQueryValue(pData, "\\StringFileInfo\\040904E4\\FileVersion",
(LPVOID*)&pBuild, &dwLen) )
Label1->Caption = AnsiString(pBuild, dwLen);
to this instead:
UINT uiLen;
char *pBuild;
if( VerQueryValue(pVersionInfo,
"\\StringFileInfo\\040904E4\\FileVersion", (LPVOID*)&pBuild, &ui) )
Label1->Caption = AnsiString(pBuild, ui);
Gambit
 

Re:Read string from DLL

"G" < XXXX@XXXXX.COM >wrote in message
Quote
Try this;
You are not doing any error checking in that code at all.
Quote
char filename[MAX_PATH];
strcpy (filename, Application->ExeName.c_str());
You are using AnsiString in the code anyway, so why not an AnsiString for
your filename variable? The ExeName is an AnsiString in the first place, so
there is no need to convert it to a char[].
Quote
unsigned long *buffer = new unsigned long[size];
'new' can thrown an exception if the memory fails to be allocated.
Quote
VerTemp = String(MajorVersion) + "."+
String(MinorVersion) + "."+
String(MajorBuild) + "."+
String(MinorBuild);
If you put the full "#.#.#.#" string in the version resources, then you do
not have to build it up manually. My earlier example code assumed the full
string was already in the resources as-is.
Gambit
 

Re:Read string from DLL

Hi Gambit,
I wasn't correcting you, I was just showing a piece of code I have that
works for me so he could put the two together and come up with something
that works for him.
Thanks for your comments though, I'll be changing my code accordingly.
Regards,
Garreth
Remy Lebeau (TeamB) wrote:
Quote
"G" < XXXX@XXXXX.COM >wrote in message
news:42bc9c91$ XXXX@XXXXX.COM ...


>Try this;


You are not doing any error checking in that code at all.


>char filename[MAX_PATH];
>strcpy (filename, Application->ExeName.c_str());


You are using AnsiString in the code anyway, so why not an AnsiString for
your filename variable? The ExeName is an AnsiString in the first place, so
there is no need to convert it to a char[].


>unsigned long *buffer = new unsigned long[size];


'new' can thrown an exception if the memory fails to be allocated.


>VerTemp = String(MajorVersion) + "."+
>String(MinorVersion) + "."+
>String(MajorBuild) + "."+
>String(MinorBuild);


If you put the full "#.#.#.#" string in the version resources, then you do
not have to build it up manually. My earlier example code assumed the full
string was already in the resources as-is.


Gambit


 

Re:Read string from DLL

salamander1965 wrote:
Quote
Aren't there some problems/complications with passing AnsiString
between an application and a DLL? It seems I saw that somewhere, but
I can't re-locate it.
Yes. If you're using Borland's tools you have to make sure the memory
manager is configured correctly. Each DLL gets a lengthy comment at the
start detailing what's needed.
Of more concern, however, is that it prevents that function being
called by code generated under different development environments.
Well...'prevents' might be a little strong but it would make it darned
difficult.
IMO it is unwise to export functions from a DLL. Stick to POD (Plain
Old Data).
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html