从C#调用C ++ dll获取System.AccessViolationException

时间:2017-06-21 16:58:20

标签: c# c++ dll native-code

我正在研究一项需要控制直流电机的项目。这些直流电机连接到2个电路板。我静态链接本机代码。这是我需要得到的代码,它是用C ++编写的。

// system defs
typedef int (Sys_Initialise)();             // find all devices connected and open USB paths to them
typedef int (Sys_GetMotorHawkCount)();      // find number of motor hawks connected
typedef int (Sys_GetSwitchingHawkCount)();  // find number of switching hawks connected
typedef int (Sys_GetServoHawkCount)();      // find number of servo hawks connected
typedef int (Sys_GetAn128Count)();          // find number of analogue 128's connected
typedef int (Sys_GetDiyHawkCount)();            // find number of DIY hawks connected
typedef int (Sys_CloseAllDevices)();            // close all open paths to USB devices

// motor hawk defs
typedef int (Motor_SetType)(int devnum, int type);              // choose motor type (stepper or DC Motors)
typedef int (Motor_RunSteps)(int devnum, int steps);                // set the number of steps to execute
typedef int (Motor_SetDCMotors)(int devnum, int M1Speed, int M1Dir, int M2Speed, int M2Dir);        // set the speed and direction of both DC motors
typedef int (Motor_GetDCMotors)(int devnum, int M1Speed, int M1Dir, int M2Speed, int M2Dir);    // get the speed and direction of both DC motors from the specified board
typedef int (Motor_GetType)(int devnum, int motor_type);                                            // get the type of motor currently configured for from the specified board
typedef int (Motor_GetStepper)(int devnum, int direction, int interval, int step_mode, int power, int steps, int run_mode, int paused);         // get the current stepper motor settings from the specified board
typedef int (Motor_GetStepsRemaining)(int devnum, int steps_remaining);         // get the current steps remaining from the specified board
typedef int (Motor_SetDigitalOutputs)(int devnum, int outputs);                 // set the digital outputs
typedef int (Motor_GetDigitalOutputs)(int devnum, int outputs);                 // get the current settings of the digital outputs
typedef int (Motor_GetDigitalInputs)(int devnum, int inputs);                   // get the current digital inputs
typedef int (Motor_SetLimitEnables)(int devnum, int m1_forward, int m1_reverse, int m2_forward, int m2_reverse);            // set the limit switch enables
typedef int (Motor_GetLimitEnables)(int devnum, int m1_forward, int m1_reverse, int m2_forward, int m2_reverse);        // get the current limit switch enable settings
typedef int (Motor_SetStepper)(int devnum, int direction, int interval, int step_mode, int power);                  // configure the stepper motor
typedef int (Motor_PauseStepper)(int devnum);                                       // temporarily pause the stepper motor
typedef int (Motor_ResumeStepper)(int devnum);                                  // resume running of the stepper motor
typedef int (Motor_StopStepper)(int devnum);                                        // stop the stepper motor and clear any remaining steps
typedef int (Motor_SetRunMode)(int devnum, int run_mode);                           // set the stepper running mode (once or continuous)
typedef int (Motor_ResetBoard)(int devnum);                                     // reset board

// switching hawk defs
typedef int (Switching_SetOutputs)(int devnum, int outputs);        // set the outputs
typedef int (Switching_GetOutputs)(int devnum, int outputs);        // get the current outputs

// servo hawk defs
typedef int (Servo_SetServos)(int devnum, int Servo1, int Servo2, int Servo3, int Servo4, int Servo5, int Servo6, int Servo7, int Servo8);          // set the servos
typedef int (Servo_GetServos)(int devnum, int Servo1, int Servo2, int Servo3, int Servo4, int Servo5, int Servo6, int Servo7, int Servo8);  // get the current servo positions
typedef int (Servo_SetOutputs)(int devnum, int outputs);        // set the outputs
typedef int (Servo_GetOutputs)(int devnum, int outputs);        // get the current outputs

// analogue 128 defs
typedef int (An128_GetAnalogueInputs)(int devnum, int *AnInputs);       // get the current analogue inputs
typedef int (An128_SetSensitivities)(int devnum, int Sens1to16,int Sens17to32,int Sens33to48,int Sens49to64,int Sens65to80,int Sens81to96,int Sens97to112,int Sens113to128 );               // set the individual sensitivities required on all inputs
typedef int (An128_GetSensitivities)(int devnum, int Sens1to16,int Sens17to32,int Sens33to48,int Sens49to64,int Sens65to80,int Sens81to96,int Sens97to112,int Sens113to128 );       // get the individual sensitivities currently set on all inputs

// DIY hawk defs
typedef int (Diy_SetOutputs)(int devnum, int outputs);          // set the outputs
typedef int (Diy_GetOutputs)(int devnum, int outputs);          // get the current outputs
typedef int (Diy_GetDigitalInputs)(int devnum, int inputs);     // get the current digital inputs
typedef int (Diy_GetAnalogueInputs)(int devnum, int inputs);        // get the current analogue inputs


///////////////////////////////////////////////
// other defs
#define STEP_MODE_FULL  1
#define STEP_MODE_HALF  2

#define POWER_ZERO          1
#define POWER_FRACTIONAL    2
#define POWER_MAX           3

#define MHK_STOPPED         0
#define MHK_FORWARD         1       // direction of DC motors
#define MHK_REVERSE         2

#define STEPPER 0           // motor hawk is driving a stepper motor
#define DCMOTORS    1           // motor hawk is driving two DC motors

#define STEPPER_RUN_ONCE        0   // run the specified steps once
#define STEPPER_RUN_CONTINUOUS  1   // run stepper continuously

当我运行调试器并在这两行代码上放置一个断点时,我得到了System.AccessViolationException。当我与制造商交谈时,他们提到将目标平台更改为32位,我做了它并没有修复任何东西。他们还提到将Calling Conventions更改为“fastcall”或“__cdecl”的“stdcall”内容。通过在.dll导入旁边放置Call​​ingConvention = CallingConvention.StdCall,我认为我做得对。我想知道你们是否知道如何解决这个问题。

[DllImport("hawk.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int Motor_GetStepsRemaining(int devnum, int steps_remaining); 

[DllImport("hawk.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int Motor_SetDigitalOutputs(int devnum, int outputs);

Motor_GetStepsRemaining(boardOneSetRunMode, stepsRemaining);

shutterSensor = Motor_SetDigitalOutputs(boardTwoShutter, CLOSE);
pressureSensor = Motor_SetDigitalOutputs(boardThreeDc, DOWN);

0 个答案:

没有答案