• C

How to run a thread in a DLL?

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
prakrithkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NosfedraCommented:
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
oferhCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prakrithkAuthor Commented:
It gives me an idea of what i was missing.
I had to wait till the thread finished.

Thanks All.
PK
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.