-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
93 lines (72 loc) · 2.16 KB
/
main.c
File metadata and controls
93 lines (72 loc) · 2.16 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define UNICODE
#include "main.h"
#include "string.h"
#include "stdio.h"
#include "oleauto.h"
#include "SerialC/SerialC.h"
HANDLE hComm = INVALID_HANDLE_VALUE;
SAFEARRAY* DLL_EXPORT __stdcall ReadRawFromComm(){
SAFEARRAYBOUND bounds = {0};
bounds.lLbound = 1;
uint8_t* arr;
uint8_t* loc;
DWORD size;
SerialC_Read(hComm, &arr, &size);
bounds.cElements = size;
//char buf[100];
//sprintf(buf, "%lu elements", bounds.cElements);
//MessageBoxA(0, buf, "DLL Message", MB_OK | MB_ICONINFORMATION);
SAFEARRAY* ret = SafeArrayCreate(VT_UI1, 1, &bounds);
SafeArrayAccessData(ret, (void**)&loc);
memcpy(loc, arr, bounds.cElements);
free(arr);
return ret;
}
void DLL_EXPORT __stdcall WriteRawToComm(SAFEARRAY** arr){
VARTYPE type;
SafeArrayGetVartype(*arr, &type);
if(type == VT_UI1){
uint8_t *data;
long uBound, lBound;
SafeArrayAccessData(*arr, (void**)&data);
SafeArrayGetUBound(*arr, 1, &uBound);
SafeArrayGetLBound(*arr, 1, &lBound);
SerialC_Write(hComm, data, (DWORD)(1 + uBound - lBound));
}else{
MessageBoxA(0, "Invalid type", "DLL Message", MB_OK | MB_ICONINFORMATION);
}
}
void DLL_EXPORT __stdcall WriteStrToComm(LPCSTR toWrite){
SerialC_Write(hComm, (uint8_t*)toWrite, strlen(toWrite));
}
BSTR DLL_EXPORT __stdcall ReadStrFromComm(){
OLECHAR* arr;
DWORD size;
SerialC_Read(hComm, (uint8_t**)&arr, &size);
BSTR ret = SysAllocString(arr);
free(arr);
return ret;
}
void DLL_EXPORT __stdcall Init(const INT32 ComID){
SerialC_Init(&hComm, (uint16_t)ComID, 9600, 8, 0, 0);
}
DLL_EXPORT BOOL APIENTRY __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
SerialC_Free(hComm);
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}