Preventing buffer overflow when calling functions

Aug 26, 2010 at 4:35am
Hello, new to the forum.

I'm pretty much a novice in c++ programming so please bear with me.

Currently I am attempting to control stepper motors from a controlling kit (http://www.pc-control.co.uk/stepperbee_info.htm)

Following the manual, I've written a simple program to run a motor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//step.cpp
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "st.h"


int main()
{
	int status;
	int steps, interval, direction, outputs;

	Type_InitStp InitStp;
	Type_RunMotor1 RunMotor1; 
	Type_StopMotor1 StopMotor1; 
	Type_RunMotor2 RunMotor2; 
	Type_StopMotor2 StopMotor2; 
	Type_SetStepMode SetStepMode; 
	Type_GetCurrentStatus GetCurrentStatus; 

	HINSTANCE HStpDll;

	HStpDll=LoadLibrary("C:\\stp.dll");

	if(HStpDll==NULL)
	{
		abort();
	}
	else
	{
		InitStp=(Type_InitStp)GetProcAddress(HStpDll,"InitStp");
		RunMotor1=(Type_RunMotor1)GetProcAddress(HStpDll,"RunMotor1");
		if(RunMotor1==NULL){abort();}//just to check
		RunMotor2=(Type_RunMotor2)GetProcAddress(HStpDll,"RunMotor2");
		StopMotor1=(Type_StopMotor1)GetProcAddress(HStpDll,"StopMotor1");
		StopMotor2=(Type_StopMotor2)GetProcAddress(HStpDll,"StopMotor2");
		SetStepMode=(Type_SetStepMode)GetProcAddress(HStpDll,"SetStepMode");
		GetCurrentStatus=(Type_GetCurrentStatus)GetProcAddress(HStpDll,"GetCurrentStatus");
	}
		
	status = InitStp();//Manual says 0  is returned if connected
                           //but I get 0 if disconnected and 1 if 
                           //connected so I think there is an error
                           //in the manual.

        steps=100;
	interval=1;
	direction=0;
	outputs=0;

	RunMotor1(steps,interval,direction,outputs);

	return 0;


}

with

1
2
3
4
5
6
7
8
//st.h (provided by manufacturer)
typedef int	(*Type_InitStp)();
typedef bool	(*Type_RunMotor1)(int steps, int interval, int direction, int outputs);
typedef bool	(*Type_StopMotor1)(int outputs);
typedef bool	(*Type_RunMotor2)(int steps, int interval, int direction, int outputs);
typedef bool	(*Type_StopMotor2)(int outputs);
typedef bool	(*Type_SetStepMode)(int M1Mode, int M2Mode);
typedef bool	(*Type_GetCurrentStatus)(int *M1Active, int *M2Active, int *M1Steps, int *M2Steps,  int *Inputs);


Everything works fine until I call the function RunMotor1 on line 51 where I always get buffer overflow error. I am currently struggling to find a resolution for this.

I've looked around for things like calling conventions (__cdecl, __stdcall) and malloc (new, free), but from what I have tried, the error still pops up.

I would be greatful if anyone could provide me tips in fixing this problem.
Thank you!
Aug 26, 2010 at 5:05am
The code looks reasonable to me. You're not doing anything with pointers other than the function pointers, and you're checking RunMotor1, so I see no reason why your code should trigger a buffer overflow. You seem to be following the documentation to the letter, which is a Good Thing, so unless the manual is obviating a critical step, such as some function that needs to be called before RunMotor1(), I see no problem on your side.
Sorry, but other than "keep doing things this way and you'll be generally alright" I have little advice for you. Contact the manufacturer, I guess?
Aug 26, 2010 at 2:51pm
Thanks for looking through my code helios!

"Contact the manufacturer, I guess?"

I will do so.

Thanks again! :)
Topic archived. No new replies allowed.