source: tol/vbtol/source/datatype.cpp @ 6d7779d

1941-docker-image-to-testdevel
Last change on this file since 6d7779d was 6d7779d, checked in by Jorge Pérez <josp.jorge@…>, 18 years ago

initial import

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 11.8 KB
Line 
1#include <windows.h>
2#include <string.h>
3
4extern BSTR ChartoBSTR(char* str);
5
6char module[101];
7short initVar = 10;
8
9// Simple Sub Call
10void __declspec(dllexport) CALLBACK TestSub()
11{
12        GetModuleFileName(NULL,(LPTSTR)module,100);
13        ::MessageBox(NULL,module,"Location of the client",0);
14}
15
16// Receives a value and returns one.
17short __declspec(dllexport) CALLBACK SimpleFunc(short value)
18{
19        return value * initVar;
20}
21
22//// Receives a double value and returns one.
23double __declspec(dllexport) CALLBACK DoubleExample(double value)
24{
25        return value * initVar;
26}
27
28// Receives a value and returns one through an argument.
29void __declspec(dllexport) CALLBACK FuncByRef(short value,short *returnVar)
30{
31        *returnVar = value * initVar;
32}
33
34// Receives a string argument and returns one.
35BSTR __declspec(dllexport) CALLBACK StringExample(BSTR stringVar)
36{
37        LPSTR buffer;
38        buffer = (LPSTR)stringVar; // The address is coerced into an LPSTR.
39        ::MessageBox(NULL,buffer,"in C++",0);
40        buffer = _strrev(buffer);
41        return(SysAllocString(stringVar)); // Since buffer has the address, the argument
42                                                                           // can still be returned.
43}
44
45//// Receives a string argument and returns a transformed one.
46BSTR __declspec(dllexport) CALLBACK MyStringExample(BSTR stringVar)
47{
48        LPSTR buffer;
49
50        buffer = (LPSTR)stringVar; // The address is coerced into an LPSTR.
51        ::MessageBox(NULL,buffer,"in C++",0);
52
53        int size = strlen(buffer)*2+4;
54    char * st = new char[size];
55        strcpy_s(st, size, buffer);
56        strcat_s(st, size, " ó ");
57        strcat_s(st, size, buffer);
58
59        BSTR str = SysAllocStringByteLen(st, strlen(st));
60        delete[] st;
61        return (str);
62}
63
64// Receives an array and returns one of its elements.
65BSTR __declspec(dllexport) CALLBACK ArrayExample(LPSAFEARRAY FAR *data,short index)
66{
67        BSTR *tempArray;
68//      index--;                        // Arrays begin at zero in C/C++ and at one in Visual Basic.
69        tempArray = (BSTR*)(*data)->pvData;
70        return(SysAllocString(tempArray[index]));
71}
72
73// Receives an array of integer and returns one of its elements.
74short __declspec(dllexport) CALLBACK MyArrayExample(LPSAFEARRAY FAR *data,short index)
75{
76        short *tempArray;
77        tempArray = (short*)(*data)->pvData;
78        return tempArray[index];
79}
80
81// Receives an array of double and returns one of its elements.
82double __declspec(dllexport) CALLBACK My2ArrayExample(LPSAFEARRAY FAR *data,short index)
83{
84        double *tempArray;
85        tempArray = (double*)(*data)->pvData;
86        return tempArray[index];
87}
88
89// Receives an array of double and modifies it.
90void __declspec(dllexport) CALLBACK MyModArrayExample(LPSAFEARRAY FAR *data)
91{
92        double *tempArray;
93        tempArray = (double*)(*data)->pvData;
94    for(int i=0; i<=5; ++i)
95                tempArray[i] *= 10;
96}
97
98// Receives an array of double and return another.
99short __declspec(dllexport) CALLBACK MyNewArrayExample(LPSAFEARRAY FAR *data)
100{
101        // Create a new 1-D array of Integers.
102  SAFEARRAY * psaiNew;
103  SAFEARRAYBOUND aDim[1];
104  aDim[0].lLbound = 1;
105  aDim[0].cElements = 8;
106  // Equivalent to: Dim aiNew(1 To 8) As Integer.
107  psaiNew = SafeArrayCreate(/*VT_I2 - short*/ /*double - */VT_R8, 1, aDim);
108  //  if (psaiNew == NULL) throw ERROR_NOT_ENOUGH_MEMORY;
109  if (psaiNew == NULL)
110        return 0;
111
112  //  if (hres = SafeArrayDestroy(psaiNew)) throw hres;
113  if (SafeArrayDestroy(*data))
114        return 0;
115
116  *data = psaiNew;
117
118  // Use array.
119  double * tempArray;
120  tempArray = (double*)(*data)->pvData;
121  for(int i=0; i<8; ++i)
122        tempArray[i] = i+10.1;
123
124  return 1;
125}
126
127// Receives a matrix of double and modifies it.
128void __declspec(dllexport) CALLBACK MyMatrixExample(LPSAFEARRAY FAR *data)
129{
130  SAFEARRAYBOUND * aDims = (*data)->rgsabound;
131  // Modify 2-D array with SafeArrayGetElement and SafeArrayGetElement.
132  long ai[2];
133  double iVal;
134  int yMin = aDims[0].lLbound;
135  int yMax = yMin + (int)aDims[0].cElements - 1;
136  int xMin = aDims[1].lLbound;
137  int xMax = xMin + (int)aDims[1].cElements - 1;
138  for (int x = xMin; x <= xMax; x++) {
139    ai[0] = x;
140    for (int y = yMin; y <= yMax; y++) {
141      ai[1] = y;
142      if (SafeArrayGetElement(*data, ai, &iVal))
143                return;
144      // Equivalent to: aiInOut(x, y) = aiInOut(x, y) + 1.
145      iVal *= 10;
146      iVal += 1000;
147      if (SafeArrayPutElement(*data, ai, &iVal))
148                return;
149    }
150  }
151}
152
153// Structure for the struc argument example.
154struct testStruct
155{
156        short shortVar;
157        unsigned char ucharVar;
158        float floatVar;
159        BSTR stringVar;
160};
161
162// Receives a structure and alters it.
163void __declspec(dllexport) CALLBACK StructExample(testStruct *data)
164{
165        data->shortVar += 1234;
166        data->ucharVar += 12;
167        data->floatVar = (float)-3.4;
168}
169
170// Receives a structure, alters it and returns one of its elements.
171BSTR __declspec(dllexport) CALLBACK MyStructExample(testStruct *data)
172{
173        BSTR tempArray;
174        data->shortVar += 1234;
175        data->ucharVar += 12;
176        data->floatVar = (float)-3.4;
177        tempArray = data->stringVar;
178//      return SysAllocString(tempArray);
179        int size = 8;
180    char * st = new char[size];
181        strcpy_s(st, size, "1234567");
182        BSTR str = SysAllocStringByteLen(st, strlen(st));
183        delete[] st;
184        return (str);
185}
186
187// Receives a structure, incorporates it in a list and
188// returns a concatenation of one of its fields.
189
190typedef struct smytestStruct
191{
192        short shortVar;
193        unsigned char ucharVar;
194        float floatVar;
195        BSTR stringVar;
196    smytestStruct * next;
197} mytestStruct;
198
199typedef mytestStruct * pstruct;
200
201pstruct head = NULL;
202
203BSTR __declspec(dllexport) CALLBACK MyListExample(testStruct *data)
204{
205        pstruct p = new mytestStruct;
206
207        LPSTR buffer;
208        buffer = (LPSTR)data->stringVar;
209        ::MessageBox(NULL,buffer,"in C++",0);
210        int size = strlen(buffer)+1;
211    char * st = new char[size];
212        strcpy_s(st, size, buffer);
213        BSTR str = SysAllocStringByteLen(st, size);
214        delete[] st;
215        p->next = NULL;
216    p->stringVar = str;
217        p->shortVar = data->shortVar;
218        p->ucharVar = data->ucharVar;
219        p->floatVar = data->floatVar;
220    if (head)
221          p->next = head;
222        head = p;
223 
224    st = new char[100];
225        st[0] = '\0';
226        for (p=head; p; p=p->next) {
227          buffer = (LPSTR)p->stringVar;
228          ::MessageBox(NULL,buffer,"in C++",0);
229          strcat_s(st, 100, buffer);
230          strcat_s(st, 100, " + ");
231        }
232        str = SysAllocStringByteLen(st, strlen(st));
233        delete[] st;
234        return (str);
235}
236
237// VARIANTS.
238VARIANT __declspec(dllexport) CALLBACK VariantExample(VARIANT data)
239{
240        short i = data.iVal;
241        VARIANT v;
242        VariantInit(&v);
243        v.vt = VT_R8;
244        v.dblVal = i * 0.1;
245        return v;
246}
247/*
248// Receives the address of a Visual Basic function and calls it.
249void __declspec(dllexport) CALLBACK CallbackExample(long Addr1)
250{
251        typedef void (__stdcall *FNPTR)(BSTR stringVar);
252        FNPTR FunctionCall;
253        FunctionCall = (FNPTR)Addr1;
254//      BSTR temp = ChartoBSTR("hello");
255        BSTR temp0 = BSTR("hello");
256//      BSTR temp = SysAllocStringByteLen("hello", 6);
257        BSTR temp = SysAllocString(temp0);
258
259//    char * st = new char[10];
260//      strcpy_s(st, 10, "hello");
261//    ::MessageBox(NULL,st,"in C++",0);
262//      BSTR temp = SysAllocStringByteLen(st, strlen(st));
263//      delete[] st;
264 
265//      FunctionCall(SysAllocString(temp));
266        FunctionCall(temp);
267//      FunctionCall(SysAllocStringByteLen(st, strlen(st)));
268//      FunctionCall(SysAllocString("hello"));
269}
270*/
271/*
272// Receives the address of a Visual Basic function and calls it.
273void __declspec(dllexport) CALLBACK CallbackExample(long Addr1)
274{
275        typedef void (__stdcall *FNPTR)(double dVar);
276        FNPTR FunctionCall;
277        FunctionCall = (FNPTR)Addr1;
278    double temp = 3.1415;
279        FunctionCall(temp);
280}
281*/
282
283// Receives the address of a Visual Basic function and calls it.
284
285typedef void (__stdcall *FNPTR)(double dVar);
286FNPTR FunctionCall;
287
288void __declspec(dllexport) CALLBACK SetCallbackExample(long Addr1)
289{
290        FunctionCall = (FNPTR)Addr1;
291}
292
293void __declspec(dllexport) CALLBACK CallbackExample()
294{
295    static double temp = 3.1415;
296        temp += 1;
297        FunctionCall(temp);
298}
299
300typedef void (__stdcall *FN2PTR)(BSTR dVar);
301
302FN2PTR Function2Call = NULL;
303
304void __declspec(dllexport) CALLBACK Set2CallbackExample(long Addr1)
305{
306  Function2Call = (FN2PTR)Addr1;
307}
308
309void __declspec(dllexport) CALLBACK Call2backExample()
310{
311//      BSTR temp = SysAllocStringByteLen("hello", 6);
312//    Function2Call(temp);
313
314        char buffer[] = "Helló BSTR";
315        ::MessageBox(NULL,buffer,"ín C++",0);
316        int size = strlen(buffer)*2+4;
317    char * st = new char[size];
318        strcpy_s(st, size, buffer);
319        strcat_s(st, size, " + ");
320        strcat_s(st, size, buffer);
321
322//      BSTR temp = SysAllocStringByteLen(st, strlen(st));
323//      p->stringVar = SysAllocStringByteLen(st, strlen(st));
324
325    BSTR temp = ChartoBSTR(st);
326        BSTR ret = SysAllocString(temp);
327        Function2Call(ret);
328        delete[] st;
329        delete[] temp;
330}
331
332// Receives a currency argument and returns one.
333__int64 __declspec(dllexport) CALLBACK CurrencyExample(__int64 c1,__int64 c2)
334{
335        return(c1 + c2);
336}
337
338#include <config.h>
339#include <tol_init.h>
340#include <tol_bgrammar.h>
341#include <tol/tol_bdatgra.h>
342#include <tol/tol_blanguag.h>
343#include <tol/tol_btxtgra.h>
344#include <tol/tol_bmatgra.h>
345
346//---------------------------------------------------------------------------
347short __declspec(dllexport) CALLBACK TolGetReal(BSTR lvalue, double * retvalue)
348//---------------------------------------------------------------------------
349{
350  LPSTR str_lvalue = (LPSTR)lvalue;
351  short ret = 0;
352
353  BSyntaxObject * so = GraReal()->EvaluateExpr(str_lvalue);
354  if (so) {
355    BDat & d = Dat(so);
356        *retvalue = d.Value();
357    DESTROY(so);
358        ret = 1;
359  }
360  return ret;
361}
362
363//---------------------------------------------------------------------------
364short __declspec(dllexport) CALLBACK TolCreateReal(BSTR name, double value)
365//---------------------------------------------------------------------------
366{
367  LPSTR str_name = (LPSTR)name;
368  short ret = 0;
369
370  new BContensDat(str_name, value, str_name);
371
372/*  if (so) {
373    BDat & d = Dat(so);
374        *retvalue = d.Value();
375    DESTROY(so);
376        ret = 1;
377  }*/
378  return ret;
379}
380
381//---------------------------------------------------------------------------
382short __declspec(dllexport) CALLBACK TolGetText(BSTR lvalue, BSTR * retvalue)
383//---------------------------------------------------------------------------
384{
385  LPSTR str_lvalue = (LPSTR)lvalue;
386  short ret = 0;
387
388  BSyntaxObject * so = GraText()->EvaluateExpr(str_lvalue);
389  if (so) {
390    BText & d = Text(so);
391    char * st = (char *)(d.String());
392
393        SysFreeString(*retvalue);
394        *retvalue = SysAllocStringByteLen(st, strlen(st));
395
396        DESTROY(so);
397        ret = 1;
398  }
399  return ret;
400}
401
402//---------------------------------------------------------------------------
403short __declspec(dllexport) CALLBACK TolGetMatrix(BSTR lvalue, LPSAFEARRAY FAR * retvalue)
404//---------------------------------------------------------------------------
405{
406  LPSTR str_lvalue = (LPSTR)lvalue;
407  short ret = 0;
408
409  if (SafeArrayDestroy(*retvalue))    // Free previous array
410    return -1;
411  BSyntaxObject * so = GraMatrix()->EvaluateExpr(str_lvalue);
412  if (so) {
413    BMat & d = Mat(so);
414    BInt cols = d.Columns();
415    BInt rows = d.Rows();
416
417        // Create a new 2-D array of Doubles
418    // Equivalent to: Dim aiNew(1 To rows, 1 To cols) As Double
419    SAFEARRAY * psaiNew;
420    SAFEARRAYBOUND aDims[2];
421    aDims[0].lLbound = 1;
422    aDims[0].cElements = cols;
423    aDims[1].lLbound = 1;
424    aDims[1].cElements = rows;
425    psaiNew = SafeArrayCreate(VT_R8, 2, aDims);
426        if (psaiNew==NULL) {                  // ERROR_NOT_ENOUGH_MEMORY
427      DESTROY(so);
428          return -1;
429        }
430
431        // Use 2-D array with SafeArrayGetElement and SafeArrayGetElement
432/*    long ai[2];
433    double iVal;
434    for (int x = 1; x <= cols; x++) {
435      ai[0] = x;
436      for (int y = 1; y <= rows; y++) {
437        ai[1] = y;
438        iVal = d(x-1,y-1).Value();
439        assert (!SafeArrayPutElement(psaiNew, ai, &iVal));
440      }
441    }
442*/
443
444        // Use 2-D array with SafeArrayPtrOfIndex, SafeArrayLock and SafeArrayUnlock
445    assert (!SafeArrayLock(psaiNew));
446        long ai[2];
447    double iVal;
448        double * piInOut;
449    for (int x = 1; x <= cols; x++) {
450      ai[0] = x;
451      for (int y = 1; y <= rows; y++) {
452        ai[1] = y;
453        iVal = d(x-1,y-1).Value();
454        assert (!SafeArrayPtrOfIndex(psaiNew, ai, (void **)&piInOut));
455                *piInOut = iVal;
456      }
457    }
458        assert (!SafeArrayUnlock(psaiNew));
459       
460        *retvalue = psaiNew;                // Assing new array
461    DESTROY(so);
462        ret = 1;
463  }
464  return ret;
465}
Note: See TracBrowser for help on using the repository browser.