I try to use a dll in C#.
My problem are the pointers (like int *TimeToWait) and the struct (TLumax_Point *Points) How can i use them.
Maybe someone can give me an Example (of safe code :-)).
// ----------------------------------------------------------
int __stdcall Lumax_SendFrame(int Handle, TLumax_Point *Points, int NumOfPoints, int ScanSpeed, int UpdateMode, int *TimeToWait)
int __stdcall Lumax_WaitForBuffer(int Handle, int Timeout, int *TimeToWait, int *BufferChanged)
//-----------------------------------------------------------
//An Example in C:
typedef struct
{
unsigned short Ch1, Ch2, Ch3, Ch4, Ch5, Ch6, Ch7, Ch8, TTL;
} TLumax_Point;
TLumax_Point Lumax_Points[4000];
typedef int __stdcall (*tLumax_SendFrame)(int, TLumax_Point *, int, int, int, int *);
typedef int __stdcall (*tLumax_StopFrame)(int);
typedef int __stdcall (*tLumax_WaitForBuffer)(int, int, int *, int *);
typedef int __stdcall (*tLumax_GetPhysicalDevices)();
typedef int __stdcall (*tLumax_OpenDevice)(int, int);
typedef int __stdcall (*tLumax_CloseDevice)(int);
tLumax_SendFrame fLumax_SendFrame;
tLumax_StopFrame fLumax_StopFrame;
tLumax_WaitForBuffer fLumax_WaitForBuffer;
tLumax_GetPhysicalDevices fLumax_GetPhysicalDevices;
tLumax_OpenDevice fLumax_OpenDevice;
tLumax_CloseDevice fLumax_CloseDevice;
void main(void)
{
int i, j, PointCounter, LumaxHandle, TimeToWait, BufferChanged;
HINSTANCE DllHandle;
DllHandle = LoadLibrary("lumax.dll"); // open DLL
if (DllHandle != NULL)
{ // DLL successfully opened -> get functions
fLumax_SendFrame = (tLumax_SendFrame)GetProcAddress(DllHandle, "Lumax_SendFrame");
fLumax_StopFrame = (tLumax_StopFrame)GetProcAddress(DllHandle, "Lumax_StopFrame");
fLumax_WaitForBuffer = (tLumax_WaitForBuffer)GetProcAddress(DllHandle, "Lumax_WaitForBuffer");
fLumax_GetPhysicalDevices = (tLumax_GetPhysicalDevices)GetProcAddress(DllHandl e, "Lumax_GetPhysicalDevices");
fLumax_OpenDevice = (tLumax_OpenDevice)GetProcAddress(DllHandle, "Lumax_OpenDevice");
fLumax_CloseDevice = (tLumax_CloseDevice)GetProcAddress(DllHandle, "Lumax_CloseDevice");
if ( (fLumax_SendFrame != NULL)
&& (fLumax_StopFrame != NULL)
&& (fLumax_WaitForBuffer != NULL)
&& (fLumax_GetPhysicalDevices != NULL)
&& (fLumax_OpenDevice != NULL)
&& (fLumax_CloseDevice != NULL))
{ // now we have all functions
i = fLumax_GetPhysicalDevices(); // get number of connected Minilumax devices
if (i > 0)
{ // there's at least 1 Minilumax device
LumaxHandle = fLumax_OpenDevice(1, 0); // open the first available Minilumax card
if (LumaxHandle != 0)
{ // card successfully opened
// now, prepare the output buffer (draw 3 lines colored R/G/B)
PointCounter = 0;
for (i = 0; i < 3; i++) // loop for colors
{
for (j = 0; j < 100; j++) // each line consists of 100 points
{
Lumax_Points[PointCounter].Ch1 = j * 600; // x position
Lumax_Points[PointCounter].Ch2 = i * 10000; // y position
Lumax_Points[PointCounter].Ch3 = (i == 0) ? 65535 : 0; // red
Lumax_Points[PointCounter].Ch4 = (i == 1) ? 65535 : 0; // green
Lumax_Points[PointCounter].Ch5 = (i == 2) ? 65535 : 0; // blue
PointCounter = PointCounter + 1;
}
}
// before we can send the frame to the Minilumax card, we have to ask if the buffer is free
do
{
// we could do something useful here, like calculation of new frame data
// ...
// ...
// ...
fLumax_WaitForBuffer(LumaxHandle, 0, &TimeToWait, &BufferChanged);
}
while (BufferChanged == 0); // repeat loop until the buffer is free
fLumax_SendFrame(LumaxHandle, Lumax_Points, PointCounter, 10000, 0, 0); // send frame
Sleep(5000); // wait 5 seconds
fLumax_StopFrame(LumaxHandle); // stop laser output
fLumax_CloseDevice(LumaxHandle); // close Minilumax device
}
}
}
}
}