473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

calling a DLL from a thread

20
Hello everyone,

I have a math function dll (MathFuncsDll.dll) with several different functions. I would like to explicitly (dynamically) load one of the functions in a new thread. I use MSFT CreateThread to create the thread and within this call there is a function, let us call it ThreadFunc, that is called. In ThreadFunc I try to load the library (MathFuncsDll.Dll) and call one of the functions in the library. Unfortunately, nothing happens.... Load Library doesn't do anything.

I'm missing something pretty fundamental.

Looking for some guidance.

Thanks,

Mark Allyn
Apr 3 '10 #1
8 3485
weaknessforcats
9,208 Expert Mod 8TB
Do you mean that LoadLibray returns zero for the HMODULE?

If so, have you sopcificed the path to rhe DLL?

Also, are you using C or C++?

If C++ you have to extern "C" on your DLL export functions or GetProcAddress won't be able to find them.

Can you post a little code snippet?
Apr 3 '10 #2
allynm
20
Hi Weaknessforcats:

Thanks for suggesting these possibilities. They were very helpful. I checked them out. I spent most of the day solving the problem, and I guess what I took away from it all was that creating threads is a bit trickier than I realized.

What it came down to was that I needed to put a WaitForSingleObject() call with the INFINITE option set. I put this call right after the call to CreateThread() inthe main(). Took a long time for me to figure it out.

But, it works now the way I expected it to. BTW, if you have some advice on when it is better to use LoadLibraryEx rather than just plain old LoadLibrary I would be grateful.

Best regards. And thanks. I'll be back for more, no doubt...

Mark Allyn
Apr 3 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
As a general rule, use the Ex version of an API function if there is one.

Since C does not have function overloading, the only way MS can revise arguments is to create a new function.

I'm not sure about your location for WaitForSingle Object.

What you should to is call CreateThread(). That gets your thread started. Next, you need to commnicate with your thread and be able to tell it when to stop. You do this in main() by a call to SetEvent(). This raises a semaphore that the thread can look for. The thread has the call to WaitForSingleObject(). The thread should wait for s short period of time and then continue.

That way your thread can run freely until you say stop.

The event requires a CRITICAL_SECTION and there are rules about this. You might check MSDN for topics on this.

Also, this material is covered in great detail in Jeffrey Richter's Windows via C/C++.
Apr 4 '10 #4
allynm
20
Hi Weaknessforcats,

Sorry I've been slow getting back to you with a reply to your last message.

In the two threads I create, all I do in each one is to load a DLL and call a function in the DLL. Because it was a read only operation, I assumed that I didn't need to enter/leave a critical section. This may be a misunderstanding I have about what happens in thread synch problems. If so, I would be grateful if you would clear up this matter.

So, all I do now is put a WaitForMultipleObjects call immediately after the lines in which I call the two threads. The WaitFor call is in the Main( ).

I have Jeff Richter's Windows via C/C++ book. It is very good, but I am a pretty green novice (as you can see) and I have found his earlier book, Advanced Windows (1997), much easier to understand. I know some of it is now obsolete, but for the sort of trivial stuff I do, it is much more comprehensible.

Thanks,

Mark Allyn
Apr 6 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Do these functions you call on these threads access any common variables? If so, you will need a CRITICAL_SECTION regardless. This is to protect against a race on these variables.

It's one thing to have readers only in a database but it's another if these readers are using the same variables in your program.
Apr 6 '10 #6
allynm
20
Hi Weaknessforcats :

The DLL that gets loaded contains an Add function---really trivial. Add does what it says: Adds two numbers. Each thread uses Add. When the threads do the addition, I pass the integers directly to the Add () function in the function signature. The values that are passed in the signature are not variables.

If the variables were global and had the same values, then I suppose I would need to worry about using CriticalSections?

Thanks,
Mark
Apr 6 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
If you are doing:

Expand|Select|Wrap|Line Numbers
  1. Add(3,5);
then there's no problem since these will be local variables in the Add() function.

That is, if Add is:

Expand|Select|Wrap|Line Numbers
  1. int Add(int, int);
there is never a problem since the arguments are always local variables.

However if you have:

Expand|Select|Wrap|Line Numbers
  1. int Add(int*, int*);
then even though the pointers are local variables, the addresses in the pointers are outside the function and therefore open to access by other threads. Now you need a critical section. Further, if this occurs in different processes, then you will need a mutex rather than a critical section. Critical sections only work for the current process.
Apr 6 '10 #8
allynm
20
Hi Weaknessforcats-

Thanks for the very nicely documented and CLEAR reply. I am doing exactly as you indicate in your first snippet. Your third snippet regarding pointers is extremely helpful. I might have tripped up on that at a future time.

I am still playing around with Mutexes (Mutices?). Richter's 97 book is very good on this subject. I was aware of the process-boundedness of critical sections, but a reminder from a pro is beneficial.

Thanks for your generous replies,

Mark Allyn
Apr 6 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
1
by: Daylor | last post by:
hi. i have main thread , and 2 others thread (t1 ,t2 for example) how can i call from the main thread, to tell t1 to exit his thread ? (calling application.exitthread from main thread, in method...
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
0
by: Ted Miller | last post by:
Hi folks, I originally posted this on microsoft.public.dotnet.framework.interop. Reposting to broaden the audience. I'm having an interop problem where my managed component is reentered from...
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
1
by: | last post by:
I'm trying to think something through and am wondering if may have some suggestions. I am building a Windows service (VStudio 2005, C#) that uses a COM component to answer a telephone call(s). ...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
9
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.