-- HOME --

Creating DLLs with Microsoft Visual C++

The basic MSW Logo Code: (The early work.)

to motor1
 ; WARNING This code may hang if there is insufficient time for one line to
 ; complete its action before the next is called.
 ; It assumes you have created the motor1.dll as outlined below and copied it to
 ; the C:\WINDOWS\SYSTEM32 folder.
 ; Hopefully it will turn on two motors for ten seconds and then turn them off.
   dllload "motor1.dll
   dllcall [v MakePortLive v 0]
   dllcall [v SetMotorSpeeds v 0]
   wait 600
   dllcall [v MotorsOn v 0]
   wait 600
   dllcall [v TurnPortOff v 0]
   dllfree
end

How to create the DLL using Microsoft Visual C++ ...

Copy all of the relevant files into a folder.

Open MS VC++ and go to New/Projects/Win 32 Dynamic-Link Library. Give your Project a name and click OK. In this example the name was: "motor1"

Select: An empty DLL Project. Accept the defaults and then use Project/Add to Project/Files to add the following files to your project:

rcx.cpp
rcx.h
motor1.cpp
motor1.def

Go to Build/Build motor1.dll to compile the DLL. The DLL will be compiled to the Motor1/Debug directory.

NOTE: You will get warnings when you compile, but you should not get any "ERRORS". Warnings are OK. Errors are fatal.

 

Compile motor1.dll and paste it into the C:\WINDOWS\SYSTEM32 directory.

The File: MOTOR1.CPP (The early work.)

#include "rcx.h"

extern "C" {MakePortLive(void) 
{
  if (!RCX_start("COM2"))   // Assumes COM2 is used
  return 0;
}
}

extern "C" {TurnPortOff(void) 
{
  RCX_stop=1;
  while (RCX_stop);  //wait until interface task has finished
  return 3;
}
}

extern "C" {SetMotorSpeeds(void) 
{
//set motor speeds
  RCX_motor_val[0]=8;
  RCX_motor_val[2]=8;
  return 1;
}
}

extern "C" {MotorsOn(void) 
{
  //switch on motors A,C
  RCX_motor_on[0]=1;
  RCX_motor_on[2]=1;
  return 2;
}
}

extern "C" {MotorsOff(void) 
{
//switch off motors A,C
RCX_motor_on[0]=0;
RCX_motor_on[2]=0;
return 2;
}
}

 

The File: MOTOR1.DEF
// (The early work.)

LIBRARY MOTOR1
DESCRIPTION

EXPORTS
MakePortLive 
SetMotorSpeeds 
MotorsOn 
TurnPortOff 
MotorsOff 

@ 1 MakePortLive
@ 2 SetMotorSpeeds
@ 3 MotorsOn
@ 4 TurnPortOff 
@ 5 MotorsOff

The code below is the latest version of the DLL. It is hard-coded for a Serial Tower connected to COM2.

WARNING... Use this code at your own risk. This is all still experimental. It has been tested on Win98. It may not operate correctly on Win95. You may also have problems with NT4, Win2000 and XP!

Download the MSWBrick.dll for COM2 here . Put the DLL in the C:\WINDOWS\SYSTEM32 directory.

Download the sample MSW Logo code here.

 

The declared DLL calls are:

TurnPortOn   
TurnPortOff   
MotorAForward 
MotorAReverse 
MotorBForward 
MotorBReverse 
MotorCForward 
MotorCReverse 
MotorAOn          
MotorBOn         
MotorCOn      
MotorAOff          
MotorBOff        
MotorCOff        
Sensor1On   
Sensor1Off   
Sensor1Value    
Sensor2On   
Sensor2Off   
Sensor2Value    
Sensor3On 
Sensor3Off         
Sensor3Value   

 

The MS Visual C++ Code

mswbrick.cpp

// March 25th 2003

#include "rcx.h"
extern "C" {TurnPortOn(void) 
{
if (!RCX_start("COM2")) // Assumes Serial Port COM2 is being used.
return 1;
else
return 33;
}
}

extern "C" {TurnPortOff(void) 
{
RCX_stop=1;
while (RCX_stop); // Wait until interface task has finished
return 2;
}
}

extern "C" {MotorAForward(void) 
{
//set motor speeds to full power
RCX_motor_val[0]=8;
return 3;
}
}

extern "C" {MotorAReverse(void) 
{
//set motor speeds to full power
RCX_motor_val[0]=-8;
return 4;
}
}

extern "C" {MotorBForward(void) 
{
//set motor speeds to full power
RCX_motor_val[1]=8;
return 5;
}
}

extern "C" {MotorBReverse(void) 
{
//set motor speeds to full power
RCX_motor_val[1]=-8;
return 6;
}
}

extern "C" {MotorCForward(void) 
{
//set motor speeds to full power
RCX_motor_val[2]=8;
return 7;
}
}

extern "C" {MotorCReverse(void) 
{
//set motor speeds to full power
RCX_motor_val[2]=-8;
return 8;
}
}

extern "C" {MotorAOn(void) 
{
//switch on motor A
RCX_motor_on[0]=1;
return 9;
}
}

extern "C" {MotorBOn(void) 
{
//switch on motor B
RCX_motor_on[1]=1;
return 10;
}
}

extern "C" {MotorCOn(void) 
{
//switch on motor C
RCX_motor_on[2]=1;
return 11;
}
}

extern "C" {MotorAOff(void) 
{
//switch off motor A
RCX_motor_on[0]=0;
return 12;
}
}

extern "C" {MotorBOff(void) 
{
//switch off motor B
RCX_motor_on[1]=0;
return 13;
}
}

extern "C" {MotorCOff(void) 
{
//switch off motor A
RCX_motor_on[2]=0;
return 14;
}
}

extern "C" {Sensor1On(void) 
{
RCX_sensor_on[1]=1;
return 15;
}
}

extern "C" {Sensor1Off(void) 
{
RCX_sensor_on[1]=0;
return 16;
}
}

extern "C" {int Sensor1Value(int Value1) 
{
Value1 = RCX_sensor_val[1];
return Value1;
}
}

extern "C" {Sensor2On(void) 
{
RCX_sensor_on[2]=1;
return 17;
}
}

extern "C" {Sensor2Off(void) 
{
RCX_sensor_on[2]=0;
return 18;
}
}
extern "C" {int Sensor2Value(int Value2) 
{
Value2 = RCX_sensor_val[2];
return Value2;
}
}

extern "C" {Sensor3On(void) 
{
RCX_sensor_on[3]=1;
return 19;
}
}

extern "C" {Sensor3Off(void) 
{
RCX_sensor_on[3]=0;
return 20;
}
}
extern "C" {int Sensor3Value(int Value3) 
{
Value3 = RCX_sensor_val[3];
return Value3;
}
}

mswbrick.def

// 25th March 2003

LIBRARY MSWBRICK
DESCRIPTION

EXPORTS
TurnPortOn                @1 TurnPortOn
TurnPortOff                @2 TurnPortOff
MotorAForward         @3 MotorAForward
MotorAReverse         @4 MotorAReverse
MotorBForward         @5 MotorBForward
MotorBReverse         @6 MotorBReverse
MotorCForward         @7 MotorCForward
MotorCReverse         @8 MotorCReverse 
MotorAOn                   @9 MotorAOn 
MotorBOn                   @10 MotorBOn 
MotorCOn                   @11 MotorCOn 
MotorAOff                   @12 MotorAOff
MotorBOff                   @13 MotorBOff
MotorCOff                   @14 MotorCOff
Sensor1On                 @15 Sensor1On
Sensor1Off                 @16 Sensor1Off
Sensor1Value            @17 Sensor1Value
Sensor2On                 @18 Sensor2On
Sensor2Off                 @19 Sensor2Off
Sensor2Value            @20 Sensor2Value
Sensor3On                 @21 Sensor3On
Sensor3Off                 @22 Sensor3Off
Sensor3Value            @23 Sensor3Value