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

调用C++;从C#获取System.AccessViolationException的dll,c#,c++,dll,native-code,C#,C++,Dll,Native Code,我在做一个需要控制直流电机的项目。这些直流电机连接到2块板上。我静态链接了本机代码。这里是我需要调用的代码,它是用C++编写的。p> // system defs typedef int (Sys_Initialise)(); // find all devices connected and open USB paths to them typedef int (Sys_GetMotorHawkCount)(); // find number of motor

我在做一个需要控制直流电机的项目。这些直流电机连接到2块板上。我静态链接了本机代码。这里是我需要调用的代码,它是用C++编写的。p>
// 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位,我这样做了,但没有修复任何问题。他们还提到将调用约定更改为“stdcall”,而不是“fastcall”或“_uucdecl”。我认为我把CallingConvention=CallingConvention.StdCall放在.dll导入旁边是正确的。我想知道你们是否知道我如何解决这个问题

[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);

好吧,我想你必须和那个图书馆的供应商一起进入下一轮。您在问题中显示的P/Invoke内容并没有揭示与您的问题相关的任何内容。AccessViolation非常指向该库中的一个bug(若只是您提供了无效的参数值,而库未能正确检查这些值的有效性…),那个么我可以向您展示更多可能有用的信息吗?我会发布更多的.DLLNo,不幸的是可能不会。我对那个图书馆一无所知。您最好的机会是与供应商的技术支持合作,或者访问一个与您正在使用的板和/或软件相关的论坛(如果存在),希望在那里您可以找到了解库的人,了解可能的问题和可能的解决方法/修复…好的,非常感谢您提供的提示。