0

I have a problem; I want to make a program which gives a random number. I don't want use the rand() function. I want to make one for me then turn it to a function for educational purposes, 0but I have a problem.

see my code:

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

#define MIN 0
#define MAX 99999

using namespace std;

typedef struct _RANDOM_INFO{
    DWORD random;
    DWORD min;
    DWORD max;
} RANDOM_INFO, * LPRANDOM_INFO;

void Error(LPSTR lpErrorMessage){
    cout << lpErrorMessage << endl;
    exit(EXIT_FAILURE);
}

void GetRandom(LPVOID lpParam){
    
    DWORD dwListSize = 10000, min = 0, max = 99999;
    LPDWORD lpRandom = (LPDWORD)lpParam;
    LPSTR lpFileSelf, lpKernel, lpNtdll;    
    HMODULE hFileSelf = NULL, hKernel = NULL, hNtdll = NULL;
    
    hFileSelf = (HMODULE) GetModuleHandle(NULL);
    hKernel = (HMODULE) GetModuleHandle("kernel.dll");
    hNtdll = (HMODULE) GetModuleHandle("ntdll.dll");
    
    lpFileSelf = (LPSTR) hFileSelf;
    lpKernel = (LPSTR) hKernel;
    lpNtdll = (LPSTR) hNtdll;
    
    while(1){
        DWORD i;
        for(i = 0; i <= dwListSize; i++){
            *lpRandom = (DWORD)lpFileSelf[i];   
        }
        i = 0;
    }
    
    return;
}

int main(int argc, char **argv)
{
    DWORD random = 0;
    
    DWORD getRandomThreadId = 0;
    
    HANDLE hGetRandomThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetRandom, &random, 0, &getRandomThreadId);
    if(hGetRandomThread == INVALID_HANDLE_VALUE)
        Error("Cannot make a random list.");
        
    getch();
    
    cout << random << endl;
    Sleep(1500);
    
    return 0;
}

The variable should get a value when and print it but I always I get 0 and a windows error can someone tell me why? and another problem when I try to use the variable hKernel in the GetRandom function I get an error too, but it works fine with hFileSelf and hNtdll! is kernel protected from reading?

Note: this is not a random number generation, it's just a way to get a number from the memory when the user clicks on the enter on their keyboard, and it's not always the same time for all users so it's not always the same pointer in memory. I hope you understand what I want to do. Sorry for my bad English. just help me to fix the problem.

Thank you

2 Answers 2

1

Your GetRandom() function does not have the correct signature for a CreateThread() callback procedure. Try this instead:

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

#define MIN 0
#define MAX 99999

using namespace std;

typedef struct _RANDOM_INFO
{
    DWORD random;
    DWORD min;
    DWORD max;
} RANDOM_INFO, * LPRANDOM_INFO;

void Error(LPSTR lpErrorMessage)
{
    cout << lpErrorMessage << endl;
    exit(EXIT_FAILURE);
}

HMODULE hFileSelf = (HMODULE) GetModuleHandle(NULL);

DWORD WINAPI GetRandomThreadProc(LPVOID lpParam)
{
    LPDWORD lpRandom = (LPDWORD) lpParam;

    DWORD dwListSize = 10000, min = 0, max = 99999;
    LPBYTE lpFileSelf = (LPBYTE) hFileSelf;

    while (1)
    {
        for (DWORD i = 0; i <= dwListSize; ++i)
        {
            *lpRandom = (DWORD) lpFileSelf[i];   
        }

        Sleep(0);
    }

    return 0;
}

int main(int argc, char **argv)
{
    DWORD dwRandom = 0;
    DWORD dwRandomThreadId = 0;

    HANDLE hGetRandomThread = CreateThread(NULL, 0, &GetRandomThreadProc, &dwRandom, 0, &dwRandomThreadId);
    if (hGetRandomThread == INVALID_HANDLE_VALUE)
        Error("Cannot make a random list.");

    do
    {
        getch();
        cout << dwRandom << endl;
    }
    while (WaitForSingleObject(hGetRandomThread, 0) == WAIT_TIMEOUT);

    CloseHandle(hGetRandomThread);

    return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank u :) it works fine :) but can u explain more the problem please?? :)
I got rid of the HMODULEs you were not using for anything and made the remaining HMODULE get obtained at app startup instead of at thread startup since its value will not change during the app's lifetime. But the real change was in the signature of GetRandom(). The original declaration did not match CreateThread's requirement. You used a type-cast to force the compiler to accept it instead of heeding the compiler's error about a mismatch. Your declaration had no return value and was using the __cdecl calling convention. So you were mismanaging the call stack...
... My change gives GetRandom() a return value and has it use the __stdcall calling convention instead.
aahhh ok i understood :) i didn't know that ^_^ as u see i'm noob develloper ;) thank u very much :)
I cannot answer that, but you shouldn't be using HMODULEs in this manner to begin with. If you want to read raw data from a file, then actually open the file for reading via CreateFile(), and then read from it using ReadFile/Ex() or CreateFileMapping()/MapViewOfFile().
|
1

i wanna make a program wich gives a random number

What you are doing has nothing to do with random number generation.

This is one way to do it: Linear Congruential Generator

6 Comments

^^ just tell me how to fix this problem :) and let me do what in my head :) and thank u for the link :)
mmmmmm i know, i'm not doing random number generation :P is a way to get a random number when the user click on enter :) its not the same time for users click on the enter :) so its not the same pointer :P i hope u understood my idea :) sorry for my bad english :)
No problem. As an example consider this part of your code: for(i = 0; i <= dwListSize; i++) *lpRandom = (DWORD)lpFileSelf[i];. You are writing 10000 times into that pointer without using the value; why?
He is using the value. He could hit the keyboard at any moment and see the value that the variable is currently holding. Or at least, that is his original intention of the code, anyway.
Less important: also let's hope that OP has his compiler optimizations disabled otherwise the main thread would see the same lpRandom (even if instead of 10000 he would use an infinite loop) because it is not volatile.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.