Thread: Explicit DLL load

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    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?
    Last edited by Kamil_; 08-03-2011 at 09:43 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about being specific?
    How To Ask Questions The Smart Way

    You may as well type that brief statement into google and start reading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    I did already, but nothing connected exactly to my problem

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    what do you mean by scope? The function exists within .dll, and i want to use it in my .cpp program
    Last edited by Kamil_; 08-03-2011 at 10:01 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Scope (C++)

    (Not a textbook writing service)

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    I'm quite familiar with those ideas. But still why it doesn't work?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?!?!?!?
    }

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    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

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you put it all in one function, what error message did you get then?

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    identifier not found

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explicit keyword
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2007, 01:57 PM
  2. Explicit keyword
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2006, 06:43 PM
  3. explicit
    By Trauts in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2003, 07:58 PM
  4. Implicit and Explicit
    By ripper079 in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2002, 12:22 PM