Board index » cppbuilder » How is a WIN32 DLL linked (bcc32 v5.5)

How is a WIN32 DLL linked (bcc32 v5.5)


2004-08-05 11:07:28 AM
cppbuilder20
How do I link this WIN32 DLL:
/* FILE: MyDLL.c ----------------------------------- */
#include <windows.h>
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved) { return 1; }
DWORD DLLFunction1(DWORD num) { return 2*num; }
/* ------------------------------------------------- */
...to this console application?
/* FILE: test_MyDLL.c ------------------------------ */
#include <windows.h>
__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);
int main(void) { DWORD ret_val=DLLFunction1(46); return 0; }
/* ------------------------------------------------- */
If I use:
DOS>bcc32 -tWD MyDLL.c
DOS>implib -c MyDLL.lib MyDLL.dll
DOS>bcc32 -tWC test_MyDLL.c MyDLL.lib
The MyDLL.dll and MyDLL.lib files are compiled but I get this error message when linking to test_MyDLL.c:
Error: Unresolved external 'DLLFunction1' referenced from ...\TEST_MYDLL.OBJ
 
 

Re:How is a WIN32 DLL linked (bcc32 v5.5)

When you get an error such as that always check that the function
declaration in the DLL matches that in the calling program and that
the function is exported from the DLL.
Here is a screen capture from my machine:
---------------------------
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
Quote
type mydll.c
#include <windows.h>
int WINAPI DllEntryPoint(
HINSTANCE hinst, unsigned long reason, void* lpReserved)
{ return 1; }
__declspec(dllexport)
DWORD __stdcall DLLFunction1(DWORD num) { return 2*num; }
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
Quote
type test_mydll.c
#include <windows.h>
__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);
int main()
{
DWORD ret_val=DLLFunction1(46);
return 0;
}
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
Quote
build
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
bcc32 -tWD MyDLL.c
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
mydll.c:
Warning W8057 mydll.c 6: Parameter 'hinst' is never used in function
DllEntryPoi
nt
Warning W8057 mydll.c 6: Parameter 'reason' is never used in function
DllEntryPo
int
Warning W8057 mydll.c 6: Parameter 'lpReserved' is never used in
function DllEnt
ryPoint
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
Quote
implib -c MyDLL.lib MyDLL.dll
Borland Implib Version 3.0.22 Copyright (c) 1991, 2000 Inprise
Corporation
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
Quote
bcc32 -tWC test_MyDLL.c MyDLL.lib
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
test_MyDLL.c:
Warning W8004 test_MyDLL.c 9: 'ret_val' is assigned a value that is
never used i
n function main
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
Quote

---------------------------
. Ed
Quote
Doug Martin wrote in message
news:4111a470$ XXXX@XXXXX.COM ...

How do I link this WIN32 DLL:

/* FILE: MyDLL.c ----------------------------------- */
#include <windows.h>
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason,
void* lpReserved) { return 1; }
DWORD DLLFunction1(DWORD num) { return 2*num; }
/* ------------------------------------------------- */

...to this console application?

/* FILE: test_MyDLL.c ------------------------------ */
#include <windows.h>
__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);
int main(void) { DWORD ret_val=DLLFunction1(46); return 0; }
/* ------------------------------------------------- */

If I use:

DOS>bcc32 -tWD MyDLL.c
DOS>implib -c MyDLL.lib MyDLL.dll
DOS>bcc32 -tWC test_MyDLL.c MyDLL.lib

The MyDLL.dll and MyDLL.lib files are compiled but I get this error
message when linking to test_MyDLL.c:

Error: Unresolved external 'DLLFunction1' referenced from
...\TEST_MYDLL.OBJ
 

Re:How is a WIN32 DLL linked (bcc32 v5.5)

How do I load a DLL at run time?
If I compile code based on the MSDN simple DLL example:
/*********************************************************/
/* FILE: functionsdll.c */
#include <windows.h>
BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
{ return TRUE; }
__declspec(dllexport) LPCTSTR DLLFunction1(void)
{ return ("Message from DLL.\n"); }
/*********************************************************/
and
/*********************************************************/
/* FILE: test_win32_runtime_dll.c */
#include <stdio.h>
#include <windows.h>
typedef LPCTSTR (*MYPROC)(void);
VOID main(VOID)
{ char buf[1];
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
LPCTSTR str=NULL;
hinstLib = LoadLibrary(TEXT("functionsdll"));
if (hinstLib) {
ProcAdd = (MYPROC) GetProcAddress(hinstLib, TEXT("DLLfunction1"));
if (ProcAdd) {
fRunTimeLinkSuccess = TRUE;
str=(ProcAdd) ();
printf("%s", str);
}
fFreeResult = FreeLibrary(hinstLib);
}
if (!fRunTimeLinkSuccess) printf("Alternative message.\n");
gets(buf); }
/*********************************************************/
with the command line compiler:
/*********************************************************/
/* FILE: MAKE.bat */
bcc32 -tWD functionsdll.c
implib functionsdll.lib functionsdll.dll
bcc32 test_win32_runtime_dll.c
/*********************************************************/
I get the message:
Alternative message.
not the message I want, which is:
Message from DLL.
Any advice? Is there a more succinct method; str=(ProcAdd) ();
is not an informative call?
The MSDN example can be found at:
msdn.microsoft.com/library/default.asp
msdn.microsoft.com/library/default.asp
"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
When you get an error such as that always check that the function
declaration in the DLL matches that in the calling program and that
the function is exported from the DLL.

Here is a screen capture from my machine:
---------------------------
C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>type mydll.c

#include <windows.h>

int WINAPI DllEntryPoint(
HINSTANCE hinst, unsigned long reason, void* lpReserved)
{ return 1; }

__declspec(dllexport)
DWORD __stdcall DLLFunction1(DWORD num) { return 2*num; }

C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>type test_mydll.c

#include <windows.h>
__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);

int main()
{
DWORD ret_val=DLLFunction1(46);
return 0;
}

C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>build

C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>bcc32 -tWD MyDLL.c
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
mydll.c:
Warning W8057 mydll.c 6: Parameter 'hinst' is never used in function
DllEntryPoi
nt
Warning W8057 mydll.c 6: Parameter 'reason' is never used in function
DllEntryPo
int
Warning W8057 mydll.c 6: Parameter 'lpReserved' is never used in
function DllEnt
ryPoint
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland

C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>implib -c MyDLL.lib MyDLL.dll

Borland Implib Version 3.0.22 Copyright (c) 1991, 2000 Inprise
Corporation

C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>bcc32 -tWC test_MyDLL.c MyDLL.lib
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
test_MyDLL.c:
Warning W8004 test_MyDLL.c 9: 'ret_val' is assigned a value that is
never used i
n function main
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland

C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>
---------------------------

. Ed

>Doug Martin wrote in message
>news:4111a470$ XXXX@XXXXX.COM ...
>
>How do I link this WIN32 DLL:
>
>/* FILE: MyDLL.c ----------------------------------- */
>#include <windows.h>
>int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason,
void* lpReserved) { return 1; }
>DWORD DLLFunction1(DWORD num) { return 2*num; }
>/* ------------------------------------------------- */
>
>...to this console application?
>
>/* FILE: test_MyDLL.c ------------------------------ */
>#include <windows.h>
>__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);
>int main(void) { DWORD ret_val=DLLFunction1(46); return 0; }
>/* ------------------------------------------------- */
>
>If I use:
>
>DOS>bcc32 -tWD MyDLL.c
>DOS>implib -c MyDLL.lib MyDLL.dll
>DOS>bcc32 -tWC test_MyDLL.c MyDLL.lib
>
>The MyDLL.dll and MyDLL.lib files are compiled but I get this error
message when linking to test_MyDLL.c:
>
>Error: Unresolved external 'DLLFunction1' referenced from
...\TEST_MYDLL.OBJ


 

{smallsort}

Re:How is a WIN32 DLL linked (bcc32 v5.5)

The call to GetProcAddress is not using the public name for the
function. Public names are formed from the source name according
to the language and calling conventions used. You can see the
public names of the exported items with this command:
tdump -m -ee functionsdll.dll
Note the prepended underscore in the name. Also note the typo in
the name used - the code you posted said Dllfunction1 but the
source name is DllFunction1, capital 'F' not lower case.
The batch file to build the program and dll uses implib to create
an import library for the function. However an import library is
used to implicitly link to a dll but this code explicitly links
to it and an import library is not used.
There are problems with that source code. You might consider
getting a book on programming.
main returns int. It does not return void. It does not have
a calling argument of type void. All functions that return a
value must have a return statement.
fFreeResult is not used.
Since 1986 (18 years!) extra parentheses are not needed around
a function pointer to make a call
str = ProcAdd(); instead of str = (ProcAdd)();
A call to gets is made with an argument of a string of length one.
This means that the gets function can overwrite memory beyond the
allocated memory in the calling argument, writing to memory the
program does not own and possibly crashing the program or the
operating system.
Here is corrected source code:
----------------------
#include <stdio.h>
#include <windows.h>
typedef LPCTSTR (*MYPROC)(void);
int main()
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fRunTimeLinkSuccess = FALSE;
LPCTSTR str;
hinstLib = LoadLibrary(TEXT("functionsdll"));
if (hinstLib)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib,
TEXT("_DLLFunction1"));
if (ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
str = ProcAdd();
printf("%s", str);
}
FreeLibrary(hinstLib);
}
if (!fRunTimeLinkSuccess)
printf("Alternative message.\n");
getchar();
return 0;
}
----------------------
You can get some introductory programs with and without DLL's,
implicitly and explicitly linked, console mode and GUI mode by
downloading this file:
www.mulroy.org/hello.zip
Unpack it into its own directory with directory creation enabled.
. Ed
"Doug Martin" < XXXX@XXXXX.COM >wrote in message
Quote

How do I load a DLL at run time?

If I compile code based on the MSDN simple DLL example:

/*********************************************************/
/* FILE: functionsdll.c */
#include <windows.h>
BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpReserved )
{ return TRUE; }
__declspec(dllexport) LPCTSTR DLLFunction1(void)
{ return ("Message from DLL.\n"); }
/*********************************************************/

and

/*********************************************************/
/* FILE: test_win32_runtime_dll.c */
#include <stdio.h>
#include <windows.h>
typedef LPCTSTR (*MYPROC)(void);
VOID main(VOID)
{ char buf[1];
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
LPCTSTR str=NULL;
hinstLib = LoadLibrary(TEXT("functionsdll"));
if (hinstLib) {
ProcAdd = (MYPROC) GetProcAddress(hinstLib,
TEXT("DLLfunction1"));
if (ProcAdd) {
fRunTimeLinkSuccess = TRUE;
str=(ProcAdd) ();
printf("%s", str);
}
fFreeResult = FreeLibrary(hinstLib);
}
if (!fRunTimeLinkSuccess) printf("Alternative message.\n");
gets(buf); }
/*********************************************************/

with the command line compiler:

/*********************************************************/
/* FILE: MAKE.bat */
bcc32 -tWD functionsdll.c
implib functionsdll.lib functionsdll.dll
bcc32 test_win32_runtime_dll.c
/*********************************************************/

I get the message:

Alternative message.

not the message I want, which is:

Message from DLL.

Any advice? Is there a more succinct method; str=(ProcAdd) ();
is not an informative call?

The MSDN example can be found at:

msdn.microsoft.com/library/default.asp

msdn.microsoft.com/library/default.asp










"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
>When you get an error such as that always check that the function
>declaration in the DLL matches that in the calling program and that
>the function is exported from the DLL.
>
>Here is a screen capture from my machine:
>---------------------------
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>type mydll.c
>
>#include <windows.h>
>
>int WINAPI DllEntryPoint(
>HINSTANCE hinst, unsigned long reason, void* lpReserved)
>{ return 1; }
>
>__declspec(dllexport)
>DWORD __stdcall DLLFunction1(DWORD num) { return 2*num; }
>
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>type test_mydll.c
>
>#include <windows.h>
>__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);
>
>int main()
>{
>DWORD ret_val=DLLFunction1(46);
>return 0;
>}
>
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>build
>
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>bcc32 -tWD MyDLL.c
>Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
>mydll.c:
>Warning W8057 mydll.c 6: Parameter 'hinst' is never used in function
>DllEntryPoi
>nt
>Warning W8057 mydll.c 6: Parameter 'reason' is never used in
>function
>DllEntryPo
>int
>Warning W8057 mydll.c 6: Parameter 'lpReserved' is never used in
>function DllEnt
>ryPoint
>Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
>
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>implib -c MyDLL.lib MyDLL.dll
>
>Borland Implib Version 3.0.22 Copyright (c) 1991, 2000 Inprise
>Corporation
>
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>bcc32 -tWC test_MyDLL.c MyDLL.lib
>Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
>test_MyDLL.c:
>Warning W8004 test_MyDLL.c 9: 'ret_val' is assigned a value that is
>never used i
>n function main
>Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
>
>C:\Documents and Settings\Administrator\My Documents\Lookat\q040
>>
>---------------------------
>
>. Ed
>
>>Doug Martin wrote in message
>>news:4111a470$ XXXX@XXXXX.COM ...
>>
>>How do I link this WIN32 DLL:
>>
>>/* FILE: MyDLL.c ----------------------------------- */
>>#include <windows.h>
>>int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason,
>void* lpReserved) { return 1; }
>>DWORD DLLFunction1(DWORD num) { return 2*num; }
>>/* ------------------------------------------------- */
>>
>>...to this console application?
>>
>>/* FILE: test_MyDLL.c ------------------------------ */
>>#include <windows.h>
>>__declspec(dllimport) DWORD __stdcall DLLFunction1(DWORD num);
>>int main(void) { DWORD ret_val=DLLFunction1(46); return 0; }
>>/* ------------------------------------------------- */
>>
>>If I use:
>>
>>DOS>bcc32 -tWD MyDLL.c
>>DOS>implib -c MyDLL.lib MyDLL.dll
>>DOS>bcc32 -tWC test_MyDLL.c MyDLL.lib
>>
>>The MyDLL.dll and MyDLL.lib files are compiled but I get this
>>error
>message when linking to test_MyDLL.c:
>>
>>Error: Unresolved external 'DLLFunction1' referenced from
>...\TEST_MYDLL.OBJ
>
>