1
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>

    void Thread1( LPVOID param)
    {
      int a;
      a = *((int *)param);
      for (int i= 0; i <10; i++)
      printf("%d\n", a);
    }

    int main()
    {
      int a =4;
      int ThreadId;
      CreateThread( 0, 0x0100, Thread1, &a, 0, &ThreadId);

      for( int i = 0; i <11; i++)
          Sleep( 1);

      return( 1);
    }

This is a simple code but I am not able to figure it out why visual studio is giving me error:

error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void *)' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Error executing cl.exe.

College Kid
  • 11
  • 1
  • 2
  • 1
    Can you change your declaration of `Thread1` to `unsigned long __stdcall Thread1(LPVOID param)`? (And return an integer?) – Kerrek SB Jun 27 '11 at 01:31
  • Yeah, I changed that and I am getting result: But why do I require unsigned long or DWORD for that case at the beginning: the function still gives error when I declare it " void WINAPI Thread1( LPVOID param)" – College Kid Jun 27 '11 at 01:47

1 Answers1

3

define as following

DWORD WINAPI MyThreadProc(LPVOID lpParameter)

CreateThread() require __stdcall calling convention.

mattn
  • 7,571
  • 30
  • 54
  • 1
    Yeah, I changed that and I am getting result: But why do I require unsigned long or DWORD for that case at the beginning: the function still gives error when I declare it " void WINAPI Thread1( LPVOID param)" – College Kid Jun 27 '11 at 01:50
  • @college-kid really? it seems that LPTHREAD_START_ROUTINE is defined as my said. http://msdn.microsoft.com/en-us/library/aa964928.aspx – mattn Jun 27 '11 at 01:56
  • That's because MyThreadProc needs to return a DWORD. Return 0 if nothing else. – Stewart Aug 24 '17 at 09:29