?
Solved

How to run  a thread in a DLL?

Posted on 2003-03-17
3
Medium Priority
?
251 Views
Last Modified: 2010-04-15
Hi,

I have written this code to start a thread in a DLL.
But it doesn't seem to work properly.
It sometimes prints the numbers in the loop and
sometimes it doesn't.(See Code below)

What is the problem? Please correct me.

PK

MyApp.cpp

#include "C:\WDriv1.h"
 
 void main() {
  StartThreads();
 }

MyDll.cpp


extern "C"  __declspec(dllexport)
 int __stdcall StartThreads( )
 {

   m_hRWThread= (HANDLE)
       _beginthreadex (NULL, 0, &RWThread, NULL, 0, &dwTStat);

   if(m_hRWThread == 0)
   {          
    printf(" read thread not created %d ",iPortNo);                        
    return 0;
   }
   else
   {
    printf(" thread created ");
   }
    return 1;
 }

 UINT WINAPI RWThread (LPVOID pArg) {

      printf("  isnide thread 0 %d ", pArg);
     for(int i = 0;i<10;i++)
          printf(" i %d ",i);
     
     return 0;
 }


MYDll.h

#ifndef _MYDLL_H_
      #define _MYDLL_H_

      #include <iostream.h>
      #include <stdio.h>
      #include <windows.h>
      #include "stdio.h"

      extern "C"  __declspec(dllexport) int __stdcall StartThreads();
 #endif
0
Comment
Question by:prakrithk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:Nosfedra
ID: 8154066
Shouldn't you use LoadLibrary before calling the StartThreads function?

Also, your code seems to be pointing that you are using an import library for the dll (adding it in the project settings ->Link tab), for implicit linking; otherwise you should call for GetProcAddress to obtain the pointer to StartThreads...

0
 
LVL 1

Accepted Solution

by:
oferh earned 300 total points
ID: 8154367
after starting the thread you should wait for it to
finish. in this case you can do something simple
like:

static bool b_finished = false;

in main:

while (!b_finished) Sleep(5);

and in the RWThread :

for(int i = 0;i<10;i++)
         printf(" i %d ",i);
b_finished = true;
0
 

Author Comment

by:prakrithk
ID: 8155827
It gives me an idea of what i was missing.
I had to wait till the thread finished.

Thanks All.
PK
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question