Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

how to create a thread and a modal dialog from a regular dll?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MechSoft - 03 Jun 2005 17:16 GMT
Hi, there,

I have a multi-threaded non-MFC program that will include a regular dll
which would be part of the user interface, possibly a dialog based interface.
I would prefer to have the user interface dll run on its own thread. How to
do that? I can't find the answer from the msdn knowledge base.

Thanks a lot in advance!

Kate
Severian [MVP] - 03 Jun 2005 17:48 GMT
>Hi, there,
>
>I have a multi-threaded non-MFC program that will include a regular dll
>which would be part of the user interface, possibly a dialog based interface.
>I would prefer to have the user interface dll run on its own thread. How to
>do that? I can't find the answer from the msdn knowledge base.

Threads and DLLs are separate things; a DLL can start a thread, and a
program can call DLL routines from different threads.

If I understand what you want to do, it's probably to call a routine
in the DLL -- StartMyDllUserInterface(), for instance -- and within
that, begin the thread (_beginthreadex) to handle the UI for your DLL.

Note that the thread will require its own message loop, and be careful
not to update UI elements from multiple threads (this causes
deadlocks).

Communication back to the main program will depend on what it expects,
and whether you're in control of how this works. I would need more
details to give definite advice.

Also, you may end up with a confusing interface if you expect things
like keyboard focus, etc., to work well between the main program and
your separate UI thread.

--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
MechSoft - 03 Jun 2005 19:24 GMT
Thank you very much for your prompt response. It is very helpful to me.

Yes, my user interface dll will export a function to the application so that
the application can call it to launch the interface. In the function, as you
named, StartMyDllUserInterface(MyInterfaceClass*), the appplication will pass
a pointer which contains all functions that the UI dll needs to communicate
with the application. Most communications are one-way from the interface to
the application. The application is like a database to the UI. The interface
Dll will also export another function to allow the application to inform the
UI of data changes.

With the helpful information from your response, I think I need to create a
user-interface thread from the exported function "StartMyDllUserInterface". I
am going to look more to figure out the details. Any advice will be
appreciated.

Kate

> >Hi, there,
> >
[quoted text clipped - 26 lines]
> Microsoft MVP, Windows SDK
> Posting email address is real, but please post replies on the newsgroup.
Severian [MVP] - 03 Jun 2005 21:48 GMT
>> >Hi, there,
>> >
[quoted text clipped - 23 lines]
>
>Thank you very much for your prompt response. It is very helpful to me.

You're quite welcome.

>Yes, my user interface dll will export a function to the application so that
>the application can call it to launch the interface. In the function, as you
>named, StartMyDllUserInterface(MyInterfaceClass*), the appplication will pass
>a pointer which contains all functions that the UI dll needs to communicate
>with the application. Most communications are one-way from the interface to
>the application. The application is like a database to the UI.

Be aware that callbacks to the application will be done in the context
of your thread! This may have unusual consequences unless the
application is carefully designed and written to handle calls from
arbitrary threads.

>The interface
>Dll will also export another function to allow the application to inform the
>UI of data changes.

Since this function will be called from a different thread than the UI
thread, it should only post messages to the UI thread or UI windows.
It should not use SendMessage or any UI call that implies SendMessage
(i.e., SetWindowText, SetDlgItemText). Otherwise, you could have two
threads updating the same UI, which is dangerous and often causes
deadlocks.

>With the helpful information from your response, I think I need to create a
>user-interface thread from the exported function "StartMyDllUserInterface". I
>am going to look more to figure out the details. Any advice will be
>appreciated.

Your UI thread will probably look much like WinMain(); it will need to
create a window (or dialog box) and start a message loop.

If the main application also has a user interface, you may need to
become familiar with such calls as AttachThreadInput(). Here are some
articles that may help:

http://support.microsoft.com/default.aspx?scid=kb;en-us;90975
http://support.microsoft.com/default.aspx?scid=kb;en-us;97925

and especially:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/m
sdn_winthr.asp


--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
MechSoft - 03 Jun 2005 23:30 GMT
Thank you! Those references are very good readings. I have learnt quite a lot
today! And I have been able to display a dialog box already from the dll.
Thanks again,

Kate

> >> >Hi, there,
> >> >
[quoted text clipped - 72 lines]
> Microsoft MVP, Windows SDK
> Posting email address is real, but please post replies on the newsgroup.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.