-
Explicit DLL load
questions covering dynamic use of DLL
I wrote a little program to load txt file, than sort out all included numbers and create output file.
I also need to add Transform.dll which contains one function i necessarily need to use. When I try to compile
I see a message error C3861: 'Transform': identifier not found, but i think i had included an identifier.
Here is the code
#include<string>
#include<fstream>
#include<iostream>
#include<vector>
#include<sstream>
#include<windows.h>
//#define DLL_EXPORT extern "C" __declspec( dllexport )
using namespace std;
typedef DWORD (*funcTransform)(DWORD);
vector<DWORD> v1;
DWORD x;
string line;
long c[600000];
unsigned int k=0;
void sort(void);
void load_file(void);
void create_output(void);
int main(){
HINSTANCE hDll=LoadLibrary("Transform.dll");
//funcTransform Transform;
if(hDll==NULL)
cout<<"Dll error"<<endl;
else
funcTransform Transform=(funcTransform)GetProcAddress(hDll,"Tran sform");
load_file();
sort();
create_output();
FreeLibrary(hDll);
}
void load_file(void){
long a;
ifstream in("input.txt");
while(getline(in,line)){
stringstream s(line);
s >> a;
c[k]=a;
k++;
}
}
void sort(void){
unsigned int i,j;
unsigned int top;
for(i=1;i<k;i++){
top=c[i];
j=i-1;
while((top<c[j])&&j>=0){
c[j+1]=c[j];
j--;
}
c[j+1]=top;
}
}
void create_output(void){
ofstream out("output.txt");
for(long d=0;d<k;d++){
x=c[d];
out<<Transform(x)<<endl;
}
}
Any Ideas?
-
How about being specific?
How To Ask Questions The Smart Way
You may as well type that brief statement into google and start reading.
-
I did already, but nothing connected exactly to my problem
-
You need to get better at thinking; particularly in thinking about scope. Where does the variable Transform exist, and where do you attempt to use it?
-
transform() is a function. I only need to use it in my last function, which creates list with ordered numbers. I firstly need to send them as parameter for transform(), it's output will be stored in output file
-
That's nice, but not relevant. (If it makes you happier to use "the name Transform" instead of "the variable Transform", then reword my previous comment as such. You still need to know how scope works.)
-
what do you mean by scope? The function exists within .dll, and i want to use it in my .cpp program
-
Scope (C++)
(Not a textbook writing service)
-
I'm quite familiar with those ideas. But still why it doesn't work?
-
If you were even a little bit familiar with those ideas, let alone quite familiar, you would know why it doesn't work. This is what you are doing:
Code:
int main() {
function_pointer_type Transform = get_function_out_of_dll;
}
int some_other_function_that_isnt_main_at_all() {
Transform(); //hey where did my variable go?!?!?!?
}
-
I understand your opinion. But I tried to keep it all in one function and it didn't work. I also tried to state it within main() and got the same error message. Therefore
I'm confused
-
When you put it all in one function, what error message did you get then?
-
-
So let's talk about scope. Here is the scope of your declaration:
Code:
if(hDll==NULL)
cout<<"Dll error"<<endl;
else
funcTransform Transform=(funcTransform)GetProcAddress(hDll,"Transform");
That's it. You declared the variable inside the if-statement, so that's all you get: a variable inside the if-statement. If you want to use it elsewhere, you should make sure the variable has some sort of larger scope, like at least the main function.
-
You're creating a function pointer variable in one function and then trying to use it incorrectly from within a different function.
That wont work for two reasons which you can work out from my previous sentence.