I am trying to use the srand and rand functions in a DLL, but for some reason I keep getting access violations (and this function worked before it was part of a DLL).
Here's a short example of my problem:
Code:
//this is the DLL
#include <windows.h>
#include <ctime>
extern "C"
{
__declspec(dllexport) int Foo(char* str)
{
int len = strlen(str);
//if I change the following three lines to this:
//str[5] = '^';
//everything works
srand(time(0));
int num = rand()%len;
str[num] = '^';
return 0;
}
}
Code:
#include <windows.h>
#include <iostream>
typedef int (*MYDLLPROC)(char*);
MYDLLPROC Foo;
int main()
{
char str[] = "Hello World!";
HMODULE hDLL = LoadLibrary("dllClassTest.dll");
Foo = (MYDLLPROC)GetProcAddress(hDLL,"Foo");
Foo(str); //access violation
std::cout<<str;
}
I'm using VS.NET 2003 if that makes a difference.