Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to run  a thread in a DLL?

Posted on 2003-03-17
3
Medium Priority
?
258 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
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

580 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