Thread: Calling inpout32.dll for port control

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Calling inpout32.dll for port control

    Hello,

    I am fairly to programming in c++. I am having trouble with using dll files. I am aware of the LoadLibrary and GetProcAddress functions. And I have tried to write some codes using them to control parallel port. I would be glad if you gave me some feedback if it is true or not.



    Code:
    #include <iostream>
    #define Data 0x378
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        oz=LoadLibrary("inpout32.dll");
        if(oz!=NULL)
                     function=GetProcAddress(oz,"out32");
        function(Data,254);     
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Thank you.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Code:
    #include <iostream>
    #define Data 0x378
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       HINSTANCE oz;  
      oz=LoadLibrary("inpout32.dll");
        if(oz!=NULL)
                     function=GetProcAddress(oz,"out32");
        function(Data,254);     
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Forgot to add instance.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You also forgot to define function. Your code also tries to execute function, whether it has been properly initialised or not. Beyond that, this is not a C++ question. It is (mostly) specific to the dll you are using, and windows.

    If you are using inpout32.dll from logix4u.net .....

    1) The function for out32 is named "Out32", not "out32". These things are case sensitive.

    2) The return value from GetProcAddress() is a void pointer. In C++, there is no implicit conversion from a void pointer to other pointer types.

    3) Inpout32.dll installs a kernel mode driver (usually as a result of calling one of the functions exported by the DLL). This means, the first time you use it, you need to be logged in to an account with administrative privileges (as that is what is required to install a kernel mode driver). If you don't do that, the dll cannot function. Once you have run it (at least) once from an account with administrative privileges, the dll (and programs using it) can be run from an account without admin privileges (unless your programs need admin privileges for other reasons).

    4) When you have finished using the functions in the dll, it is usually a good idea to call FreeLibrary().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Thank you for your answer.
    If I understood right, I have to define function also. What I cant get correctly is using GetProcAddress then. Do I have to define a new function for usage of Out32 ? Or can I just use Out32 freely after I have handled the inpout32.dll. I feel really confused about these concepts. Other than these I understand your post completely. Thank you. Id be just glad if you remedied my confusion above.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    And Also , I am having problem compiling loadlibrary codes with dev c++. After you said that my problem is about windows and specific dll, I think that the compiler might be having problems recognizing LoadLibrary and Getp... functions. Could this be true? It doesnt even recognize HINSTANCE. Maybe I need additional libraries .
    Last edited by ozumsafa; 08-18-2011 at 06:00 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you bother to #include <windows.h>?

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Allright that could solve the problem if I have the windows.h . Whu so aggresive , I ask because I am kind of confused about which is actually for windows and which is for c++ syntax. I have some idea about c++ , but I think I dont have enough background about the relations between file formats, os and language.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    LoadLibrary() is a win32 function, so most compilers targeting windows will be able to use it. You need to include appropriate declarations (typically via a header file like <windows.h>, as suggested by tabstop) though.

    When getting a pointer via GetProcAddress(oz, "Out32"), you need to convert the return value into a function pointer of the appropriate type (IIRC a stdcall function that accepts an argument of type short, and returns a short). Once you have such a pointer, you can use it until you call FreeLibrary() to release the DLL instance.

    If my description above seems like gibberish to you, then ...... If you are still in the early stages of learning C++, you will probably want to do something a bit more elementary than loading dll functions. Loading dll functions is not difficult, but it does involve some specific techniques in C++. If you don't understand C++, it is really hard to understand how to work with dlls, using C++ code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Thanks again. No I did not think feel that it was gibberish to me. Although I dont claim to be someone about c++. Anyway let's see If I can see the logic correctly then. I will create a stdcall function to be able to use Out32 with the new pointer function.

    I will propose this code :
    Code:
       HINSTANCE oz;  
      oz=LoadLibrary("inpout32.dll");
    
    typedef short (_stdcall function)(char *a,short b);
    function *func;
    func=GetProcAddress(oz,"Out32");
    I understand types of variables and functions above brief. However I might have some issues about them. Thats why we ask questions right : ). So What I understood is ,
    I define a function definer with stdcall which has the same arguments with the function I want to call from the dll file, so that I can replicate it ?
    Last edited by ozumsafa; 08-18-2011 at 06:34 AM.

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    When I use this code :
    Code:
    #include <windows.h>
    #include <iostream>
    #define Data 0x378
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        HINSTANCE oz;
        oz=LoadLibrary("inpout32.dll");
        typedef int (_stdcall function)(char *a,int b);
    function *func;
    func=GetProcAddress(oz,"Out32");
        func(Data,254);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I get this error :

    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':
    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':
    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':
    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':


    Now I have done this and I dont get any errors. Although I cant try coz this is a laptop .

    Code:
    #include <windows.h>
    #include <iostream>
    #define Data 0x378
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        HINSTANCE oz;
        oz=LoadLibrary("inpout32.dll");
        typedef int (_stdcall function)();
    function *func;
    func=GetProcAddress(oz,"Out32");
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    However the code I want to apply is this :
    Code:
    #include <windows.h>
    #include <iostream>
    #define Data 0x378
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        HINSTANCE oz;
        oz=LoadLibrary("inpout32.dll");
        typedef int (_stdcall function)();
    function *func;
    func=GetProcAddress(oz,"Out32");
        func(Data,254);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Because I only want pin 2 to go high in parallel port. However I get this error :
    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':
    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':
    C:\Dev-Cpp\main.cpp In function `int main(int, char**)':

    I feel like my first code makes sense as Out32(PORTvariable,value) is like this. Giving the value to the port adress which is 0x378, basically a char pointer ?
    Last edited by ozumsafa; 08-18-2011 at 06:43 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    5) inpout32.dll was only meant to be a transitionary hack to allow somewhat easier upgrades of ancient DOS programs to run on a protected mode operating system.
    If you're writing new code, you should really be using the provided OS API's for accessing devices.
    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.

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Quote Originally Posted by Salem View Post
    5) inpout32.dll was only meant to be a transitionary hack to allow somewhat easier upgrades of ancient DOS programs to run on a protected mode operating system.
    If you're writing new code, you should really be using the provided OS API's for accessing devices.
    Actually what I am doing has no aim. I just want to be able to use inpout32.dll. Just to learn how it is used. Could you comment on my last post so that I can see my mistakes, not that I am not trying by myself.

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I guess it is enough help for me ? : )

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Based on the documentation for inpout32, the typedef for function needs to be
    Code:
       typedef short _stdcall (*function)(short, short);
    In C++, the types of all arguments and return value of a function are important (unlike the deprecated feature of C where, if you specify an empty argument list, the compiler allows an arbitrary set of arguments). Note that the first argument is passed by value as a short, not as a pointer.

    It still pays to check the return values from LoadLibrary() and GetProcAddress() to see if they succeeded. If they fail, and you proceed anyway, the results are rather unpredictable.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Quote Originally Posted by grumpy View Post
    Based on the documentation for inpout32, the typedef for function needs to be
    Code:
       typedef short _stdcall (*function)(short, short);
    In C++, the types of all arguments and return value of a function are important (unlike the deprecated feature of C where, if you specify an empty argument list, the compiler allows an arbitrary set of arguments). Note that the first argument is passed by value as a short, not as a pointer.

    It still pays to check the return values from LoadLibrary() and GetProcAddress() to see if they succeeded. If they fail, and you proceed anyway, the results are rather unpredictable.
    Thank you for the help. I will try , study and get back if I fail further.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Control BaudRate USB port
    By doia in forum C Programming
    Replies: 5
    Last Post: 03-19-2010, 05:12 PM
  2. parallel port control prblem
    By frank_hugo_1100 in forum C Programming
    Replies: 1
    Last Post: 02-26-2010, 01:00 AM
  3. Port control
    By asahin11 in forum C Programming
    Replies: 4
    Last Post: 04-26-2005, 10:54 AM
  4. Which port used MS Win XP remote control?
    By gicio in forum Tech Board
    Replies: 0
    Last Post: 10-23-2002, 08:45 AM
  5. serial port control
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2002, 06:44 PM