N
N
Nulltiton2022-03-02 22:42:52
C++ / C#
Nulltiton, 2022-03-02 22:42:52

How to fix "Frame not in module" error when creating thread?

Created a process that should perform the function of calculating the factorial, however, when trying to run, the error "The frame is not in the module" appears. What could be the reason for this and how to fix it?

int gn = 0;
scanf_s("%d", &gn);
HANDLE h = CreateThread(
    NULL,
    0,
    factorial(gn),
    NULL,
    NULL,
    NULL
);
CloseHandle(h);
return 0;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2022-03-02
@jcmvbkbc

"The frame is not in the module." Tell me what it can be connected with
HANDLE h = CreateThread(
    NULL,
    0,
    factorial(gn),
    NULL,
    NULL,
    NULL
);

The third parameter of CreateThread is a pointer to a function that will be run on the created thread. And you probably have something else. If you wanted to run the factorial function on a thread, it must 1) have a specific prototype ( like this ), and 2) you must pass its address to the CreateThread function, and not the result of its call (for example, like this: CreateThread(NULL, 0, factorial, NULL, NULL, NULL)). 3) if you make these two changes, you will also have to redo the parameter passing to the factorial function and getting the result of its work.

N
Nulltiton, 2022-03-03
@Nulltiton

I solved the problem with the help of a global one, I used an example from Pobegailo's book - "System Programming in Windows".
The code:

#include <stdio.h>
#include <Windows.h>

int n;

DWORD WINAPI Add(int Num) {
  printf("Thread is started\n");
  n += Num;
  printf("Thread is finished\n");
}

int main() {

  int inc = 10;
  HANDLE hThread;
  DWORD IDThread;

  printf("n = %d\n", n);

  hThread = CreateThread(
    NULL,
    0,
    Add,
    (void*)inc,
    0,
    &IDThread);

  if (hThread == NULL)
    return GetLastError();

  WaitForSingleObject(hThread, INFINITE);
  CloseHandle(hThread);

  printf("n = %d\n", n);

  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question