Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Developing my first DLL, some basics needs clarifying


This question is not answered.


Permlink Replies: 16 - Last Post: May 24, 2016 12:49 PM Last Post By: Goran Ekstrom Threads: [ Previous | Next ]
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Developing my first DLL, some basics needs clarifying  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2015 7:50 AM
Hi,
I am doing a DLL in XE8 Pro. that is targeted for "any" windows programming environment, i.e. any that supports loading a WinDll. I have some question about the bascis:

1: I am guessing that I need to produce both 32 and 64 bit versions, i.e. a 32 bit app always loads a 32 bit DLL and a 64 app loads 64 bit DLL:s, regardless of the OS type. Correct?

2: What about data types across the DLL boundry? Is it only the basic C-types that are safe/functional?

3: Is it best to use the WINAPI's own data type declarations for data and functios, e.g. BYTE instead of UNSIGNED CHAR?

4: I do not want to include VCLin my DLL which leaves the "Multithreaded" option in the New Project wizard the only one available. What does that mean exactly, is it the use of MT safe API:s such as basic C string functions or something else?

5: Will the main "extern "C" int _libmain(unsigned long reason)" work "in all Environments", i.e. get called regardless of which tool is used? MSDN uses DLLMain.

6: Can I include any WINAPI functionality in a DLL, i.e.create threads, etc. and in the extreme build a complete WINAPI-only windows app?

Regards
Goran Ekstrom
Apodosis AB
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Developing my first DLL, some basics needs clarifying [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2015 11:17 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

1: I am guessing that I need to produce both 32 and 64 bit versions,
i.e. a 32 bit app always loads a 32 bit DLL and a 64 app loads 64 bit
DLL:s, regardless of the OS type. Correct?

Yes.

2: What about data types across the DLL boundry? Is it only the basic
C-types that are safe/functional?

Yes.

3: Is it best to use the WINAPI's own data type declarations for data
and functios, e.g. BYTE instead of UNSIGNED CHAR?

That is not strictly necessary, but it is useful.

4: I do not want to include VCLin my DLL which leaves the
"Multithreaded" option in the New Project wizard the only one available.
What does that mean exactly, is it the use of MT safe API:s such as
basic C string functions or something else?

It means the linker will use the multi-threaded version of the RTL.

5: Will the main "extern "C" int _libmain(unsigned long reason)" work
"in all Environments", i.e. get called regardless of which tool is
used? MSDN uses DLLMain.

Yes, it will get called. The actual name of the function is irrelevant.
What is important is that the compiler/linker puts the function's address
in the DLL's PE header so the OS can find it when needed.

6: Can I include any WINAPI functionality in a DLL, i.e.create threads,
etc. and in the extreme build a complete WINAPI-only windows app?

For the most part, yes. However, there are restrictions on what you are
allowed to call inside of _libmain() itself. Pretty much anything that is
in a DLL other than kernel32.dll is off-limits (if that DLL is not already
loaded), as well as various kernel32 functions including LoadLibrary/Ex(),
FreeLibrary(), CreateProcess(), CreateThread(), ExitThread(), GetStringType/Ex(),
etc. See MSDN for more details:

Dynamic-Link Library Entry-Point Function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682596.aspx

Dynamic-Link Library Best Practices
https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971.aspx

You can export your own functions from your DLL for hosting apps to call,
and then your functions will not be restricted in what they can call.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2015 12:02 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:...

Thanks Remy, I am on the right track then. The DLL will be a wrapper for the WinUSB.DLL functionality and provide high level helper functions for a specific product.

One more question, the DLL_THREAD_ATTACH/DETACH events, will they be called if ANY thread is created within the app or only if the new thread loads the DLL?

Regards
Goran
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Developing my first DLL, some basics needs clarifying [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2015 12:49 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

One more question, the DLL_THREAD_ATTACH/DETACH events, will they
be called if ANY thread is created within the app or only if the new
thread loads the DLL?

It is called for every thread. This is stated in the documentation I directed
you to:

Dynamic-Link Library Entry-Point Function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682596.aspx

Calling the Entry-Point Function

The system calls the entry-point function whenever any one of the following
events occurs:

- A process loads the DLL. For processes using load-time dynamic linking,
the DLL is loaded during process initialization. For processes using run-time
linking, the DLL is loaded before LoadLibrary or LoadLibraryEx returns.

- A process unloads the DLL. The DLL is unloaded when the process terminates
or calls the FreeLibrary function and the reference count becomes zero. If
the process terminates as a result of the TerminateProcess or TerminateThread
function, the system does not call the DLL entry-point function.

- A new thread is created in a process that has loaded the DLL. You can use
the DisableThreadLibraryCalls function to disable notification when threads
are created.

- A thread of a process that has loaded the DLL terminates normally, not
using TerminateThread or TerminateProcess. When a process unloads the DLL,
the entry-point function is called only once for the entire process, rather
than once for each existing thread of the process. You can use DisableThreadLibraryCalls
to disable notification when threads are terminated.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2015 1:31 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
It is called for every thread. This is stated in the documentation I directed
you to:...

Ahh, missed that one, thanks!
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 6:33 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
2: What about data types across the DLL boundry? Is it only the basic
C-types that are safe/functional?

Yes.

Is there any safe way to use structs over the DLL boundry? My concern is the tool-dependent struct padding. EDIT: Is pragma pack(x) an option?

Also, is it possible to use the Delphi RTL (System) in a non-VCL DLL? I am getting unresolved in linking when using the AnsiString class.

Regards
Goran

Edited by: Goran Ekstrom on Aug 25, 2015 4:28 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 9:43 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

Is there any safe way to use structs over the DLL boundry?

Yes (plenty of C APIs use structs). You have to make sure that the struct
only holds POD types, and you should be explicit about the struct's alignment.

Is pragma pack(x) an option?

Yes.

Also, is it possible to use the Delphi RTL (System) in a non-VCL DLL?

No.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 11:00 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Goran wrote:

Is there any safe way to use structs over the DLL boundry?

Yes (plenty of C APIs use structs)...

Does that mean that any struct defined in a WINAPI H-file is safe "straight out of the box"?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit][Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 11:10 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

Does that mean that any struct defined in a WINAPI H-file
is safe "straight out of the box"?

Yes, because the Win32 API is a C-based API, offering wide portability between
different compilers.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit][Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 11:33 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
OK.

Another one. is it possible to debug the DLL and the test/host EXE in the same Project? I now have a GroupProj with the host EXE and DLL in the same Group.

I also noticed that if I build the DLL as 64 bit there is no LIB file produced and the resulting DLL is 4x the size compared to the 32 bit one, is that normal?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Developing my first DLL, some basics needs clarifying [Edit][Edit][Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 12:03 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

Another one. is it possible to debug the DLL and the test/host EXE in
the same Project? I now have a GroupProj with the host EXE and DLL in
the same Group.

If you specify a Host EXE in the DLL project and then run the DLL project,
it will run the Host EXE and attach the debugger to the DLL source when the
DLL is loaded into memory, and then you can debug the DLL as needed.

If you run the Host EXE project instead, you should be able to step into
the DLL source if the DLL source path is in the Host EXE project's Search
Path.

I also noticed that if I build the DLL as 64 bit there is no LIB file
produced and the resulting DLL is 4x the size compared to the 32 bit
one, is that normal?

I don't create 64bit DLLs, so I cannot answer that.

--
Remy Lebeau (TeamB)
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit][Edit][Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2015 12:45 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
If you run the Host EXE project instead, you should be able to step into
the DLL source if the DLL source path is in the Host EXE project's Search
Path.

Worked just fine.
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 24, 2016 11:03 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Yes (plenty of C APIs use structs). You have to make sure that the struct
only holds POD types, and you should be explicit about the struct's alignment.

When it comes to the DLL H-file function signatures and PODs, should I use base keywords such as char, int, etc. or is it OK to use the WinAPI typedefs such as UINT?

Also, is a 32 bit float always the same "float" or are there format issues?
david hoke

Posts: 616
Registered: 2/9/07
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 24, 2016 11:28 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran Ekstrom wrote:

Yes (plenty of C APIs use structs). You have to make sure that the
struct only holds POD types, and you should be explicit about the
struct's alignment.

When it comes to the DLL H-file function signatures and PODs, should
I use base keywords such as char, int, etc. or is it OK to use the
WinAPI typedefs such as UINT?

Not sure there's an abolutely correct answer to your question, not
knowing enough about your .dll's purpose, but here are some musings...

Regarding char, what is your intended use? Is your code, and/or your
clients code, going to be affected by unicode/widechar usage? Will the
DLL work, or is it intended to work, in either case? TCHAR definitions
might be something to explore (tchar.h) if you're going to be
supporting textual characters.

Regarding other numeric types, are your clients going to always be
including enough of windows api headers to see those types, or could
your .dll be used by code that otherwise needs to know nothing at all
about windows headers? Could some clients of your .dll utilize your
.dll without reference to anything else in windows (perhaps your dll
provides only some sort of computational facilities)?

I have had a tendency for numeric types to use entities from stdint.h
in libraries that may have potential for cross-platform use, both
windows 32bit and 64bit, as well as possibly on linux. That should
exist with modern c compilers, and will maintain consistent entity
sizes as well, whereas some of the standard c types might morph between
different 'size' (32bit, 64bit, will there be 128bit) platforms.

What client languages will be consuming your .dll? Only builder C/xx
clients, or possibly 'foreign' languages or compilers? If foreign,
then the windows types might be appropriate, as on windows probably all
of those clients have compatible datatype definitions to be used for
(declaring and) consuming the services of your .dll.



Also, is a 32 bit float always the same "float" or are there format
issues?

I think, outside of some very old microsoft basic code (possibly all
pre-windows), that a 32bit float can be expected, on windows, to be the
IEEE standard.
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 24, 2016 12:49 PM   in response to: david hoke in response to: david hoke
Hi David,
in theory and for the future, the DLL should be as general as possible for "any" development tool. In the current case I am collaborating with a developer that uses LabView, a graphical programming environment that is "capable of using" windows DLL:s.

I am guessing that any programming language/tool intended for producing windows EXEs must be WinAPI-aware, at least if they are not completely isloated from WinAPI.

The DLL itself will be communicating over USB with a measurement instrument and so far plain 7 bit ASCII is required for strings.

I have had a tendency for numeric types to use entities from stdint.h
in libraries that may have potential for cross-platform use, both
windows 32bit and 64bit, as well as possibly on linux. That should
exist with modern c compilers, and will maintain consistent entity
sizes as well, whereas some of the standard c types might morph between
different 'size' (32bit, 64bit, will there be 128bit) platforms.

That is really the origin of this issue, use "industry standards" or the WinAPI typedefs for the same. I am leaning towards "industry standards".

Also, is a 32 bit float always the same "float" or are there format
issues?
I think, outside of some very old microsoft basic code (possibly all
pre-windows), that a 32bit float can be expected, on windows, to be the
IEEE standard.

Good, I'll go with that then.
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 7:48 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
New issues
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: Developing my first DLL, some basics needs clarifying  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2015 11:02 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
More stuff
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02