Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ pointers and DLL linking

C++ pointers and DLL linking

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dAvId_BotMan
    wrote on last edited by
    #1

    Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening? Thanks, David Kirby /*-----------------------------------------------------------------------------------------------*/ #include "CXBOXController.h" #include <iostream> #include <windows.h> #include <tchar.h> #include <cstdlib> typedef bool     (*Type_InitMotoBee)();                                        typedef bool     (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void     (*Type_Digital_IO)(int *inputs, int outputs); using namespace std; HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); CXBOXController* Player1; int main(int argc, char* argv[]) {      Player1 = new CXBOXController(1);      system("Color 02");       system("title LANbot Host - Local Motor Control Trial");       cout << "      __      _            __ _               _            |\n"           << "      / /   /_\\      /\\ \\ \\ |__   ___ | |_         |\n"             << "   / /   //_\\\\   /   \\/ / '_ \\ / _ \\| __|      |\n"             << "   / /___/   _   \\/ /\\   /| |_) | (_) | |_         |\n"             << "   \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \

    _ P 2 Replies Last reply
    0
    • D dAvId_BotMan

      Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening? Thanks, David Kirby /*-----------------------------------------------------------------------------------------------*/ #include "CXBOXController.h" #include <iostream> #include <windows.h> #include <tchar.h> #include <cstdlib> typedef bool     (*Type_InitMotoBee)();                                        typedef bool     (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void     (*Type_Digital_IO)(int *inputs, int outputs); using namespace std; HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); CXBOXController* Player1; int main(int argc, char* argv[]) {      Player1 = new CXBOXController(1);      system("Color 02");       system("title LANbot Host - Local Motor Control Trial");       cout << "      __      _            __ _               _            |\n"           << "      / /   /_\\      /\\ \\ \\ |__   ___ | |_         |\n"             << "   / /   //_\\\\   /   \\/ / '_ \\ / _ \\| __|      |\n"             << "   / /___/   _   \\/ /\\   /| |_) | (_) | |_         |\n"             << "   \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      This would be too much work for somebody trying to help you. It is for me. So you should consider drilling this down to a more specific area. These are some comments from me. Don't use new unless you really need it. You are using too many system function calls. The code is not properly indented. You are not checking return values for errors.

      «_Superman_» I love work. It gives me something to do between weekends.

      D 1 Reply Last reply
      0
      • _ _Superman_

        This would be too much work for somebody trying to help you. It is for me. So you should consider drilling this down to a more specific area. These are some comments from me. Don't use new unless you really need it. You are using too many system function calls. The code is not properly indented. You are not checking return values for errors.

        «_Superman_» I love work. It gives me something to do between weekends.

        D Offline
        D Offline
        dAvId_BotMan
        wrote on last edited by
        #3

        Thanks for the reply. Im getting and error with these... typedef bool     (*Type_InitMotoBee)();                                   typedef bool     (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void     (*Type_Digital_IO)(int *inputs, int outputs); HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); Type_InitMotoBee InitMotoBee; Type_SetMotors SetMotors; Type_Digital_IO Digital_IO; InitMotoBee = (Type_InitMotoBee)GetProcAddress(MHtbDLL, "InitMotoBee"); SetMotors = (Type_SetMotors)GetProcAddress(MHtbDLL, "SetMotors"); Digital_IO = (Type_Digital_IO)GetProcAddress(MHtbDLL, "Digital_IO"); This is where the program calls the DLL, makes pointers, etc. After I use SetMotors(); so many times, the program is terminated for an access violation... I cant figure it out. If i comment out everything that envolves the DLL, my program works perfectly. Thanks, David

        _ 1 Reply Last reply
        0
        • D dAvId_BotMan

          Thanks for the reply. Im getting and error with these... typedef bool     (*Type_InitMotoBee)();                                   typedef bool     (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void     (*Type_Digital_IO)(int *inputs, int outputs); HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); Type_InitMotoBee InitMotoBee; Type_SetMotors SetMotors; Type_Digital_IO Digital_IO; InitMotoBee = (Type_InitMotoBee)GetProcAddress(MHtbDLL, "InitMotoBee"); SetMotors = (Type_SetMotors)GetProcAddress(MHtbDLL, "SetMotors"); Digital_IO = (Type_Digital_IO)GetProcAddress(MHtbDLL, "Digital_IO"); This is where the program calls the DLL, makes pointers, etc. After I use SetMotors(); so many times, the program is terminated for an access violation... I cant figure it out. If i comment out everything that envolves the DLL, my program works perfectly. Thanks, David

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Check for any uninitialized or dangling pointers inside SetMotors. Check if there is any difference in behavior in Debug and Release builds. Also set the warning level to the highest level in the project properties.

          «_Superman_» I love work. It gives me something to do between weekends.

          D 1 Reply Last reply
          0
          • _ _Superman_

            Check for any uninitialized or dangling pointers inside SetMotors. Check if there is any difference in behavior in Debug and Release builds. Also set the warning level to the highest level in the project properties.

            «_Superman_» I love work. It gives me something to do between weekends.

            D Offline
            D Offline
            dAvId_BotMan
            wrote on last edited by
            #5

            How exactly would i Check for dangling pointers in SetMotors? If its any help, I can tell you that When i run the app in 32bit windows xp OS, the app closes after two successful SetMotors(); commands. In 64bit windows vista OS desktop, the app runs well for quite a while before closing for the same reason. I'm checking for release/debug differences now.

            _ 1 Reply Last reply
            0
            • D dAvId_BotMan

              How exactly would i Check for dangling pointers in SetMotors? If its any help, I can tell you that When i run the app in 32bit windows xp OS, the app closes after two successful SetMotors(); commands. In 64bit windows vista OS desktop, the app runs well for quite a while before closing for the same reason. I'm checking for release/debug differences now.

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              Are you allocating memory dynamically? If so, check if they are being released properly. Also, check for buffer overflows. If you're using string functions, use the secure version that end with _s.

              «_Superman_» I love work. It gives me something to do between weekends.

              D 1 Reply Last reply
              0
              • _ _Superman_

                Are you allocating memory dynamically? If so, check if they are being released properly. Also, check for buffer overflows. If you're using string functions, use the secure version that end with _s.

                «_Superman_» I love work. It gives me something to do between weekends.

                D Offline
                D Offline
                dAvId_BotMan
                wrote on last edited by
                #7

                Sorry, I'm new at including DLL's. I have seen "Buffer Overflow" show up as an error, but from what i can tell I fixed that. I'm pretty sure that when that occured, the compiler (Visual Studio 2008) failed to compile the app and gave me the buffer overflow error. that is no longer the case. I am not allocating memory dynamically for the DLL. there is no difference between Release and Debug. It   is handling itself after I call the DLL, make the pointers, and GetProcAddress. I just use the functions... the functions are used as follows: SetMotors(x,x,x,x,x,x,x,x,x) InitMotoBee();

                1 Reply Last reply
                0
                • D dAvId_BotMan

                  Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening? Thanks, David Kirby /*-----------------------------------------------------------------------------------------------*/ #include "CXBOXController.h" #include <iostream> #include <windows.h> #include <tchar.h> #include <cstdlib> typedef bool     (*Type_InitMotoBee)();                                        typedef bool     (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void     (*Type_Digital_IO)(int *inputs, int outputs); using namespace std; HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); CXBOXController* Player1; int main(int argc, char* argv[]) {      Player1 = new CXBOXController(1);      system("Color 02");       system("title LANbot Host - Local Motor Control Trial");       cout << "      __      _            __ _               _            |\n"           << "      / /   /_\\      /\\ \\ \\ |__   ___ | |_         |\n"             << "   / /   //_\\\\   /   \\/ / '_ \\ / _ \\| __|      |\n"             << "   / /___/   _   \\/ /\\   /| |_) | (_) | |_         |\n"             << "   \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \

                  P Offline
                  P Offline
                  Pavan_Putra
                  wrote on last edited by
                  #8

                  It seems that the access violation is due to Dangling pointers. These kind of error will not be repeatable 100% ,since will be depending on size of your RAM ( when your OS tries to get back the memory which is assigned to some one else).If you have Dev-Partner with your studio , you can find the code problems or try to make sure that there is delete() for every new().You can use run time checker for this task , It's evaluation version may help you in Finding leaks.

                  It's not enough to be the best, when you have capability to be great....

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups