home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------*
- Andreas Weidinger (c) 1995
- EDV-Beratung und Entwicklung
- Glashⁿttenstra▀e 41
- 40627 Dⁿsseldorf
- Germany
- Tel: (0211) 273988
- CompuServe: 100416,2775
-
- IT WOULD BE NICE IF ANYBODY HOW HAD SIMILAR PROBLEMES OR HAS FOUND
- OUT A MORE EASIER WAY TO PASS ANY DATA FROM VB TO A DLL AND BACK
- CONTACT ME:
-
- *------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------*/
- #define NOCOMM
- /*------------------------------------------------------------------------*/
- /*StandardHeader*/
- #include<windows.h>
-
- /*ExternObjectHeader*/
- /*------------------------------------------------------------------------*/
- /* Include headerfile for VB-API-Library*/
- /*------------------------------------------------------------------------*/
- #include"vbapi.h"
- /*ApplicationHeader*/
- #include"vbdllcom.h"
- /*------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------*/
- /* Function Prototypes */
- /*------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------*/
- SHORT FAR PASCAL LibMain
- (HANDLE hModule, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine);
- #ifdef __cplusplus
- extern "C" {
- #endif
- SHORT DLLAPI WEP (SHORT nSystemExit);
- #ifdef __cplusplus
- }
- #endif
- /*------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------*/
- /* Global Variables
- /*------------------------------------------------------------------------*/
- /*------------------------------------------------------------------------*/
- static HANDLE hInstance;
- /*-------------------------------------------------------------------------*
- *
- * Function: DLL Library EntryFunction
- *
- * Parameter: hModule - Handel
- * wDataSeg - Word
- * wHeapSize - Word
- * lpszCmdLine - String
- *
- * Return: SHORT
- * TRUE
- *
- * Comment:
- *-------------------------------------------------------------------------*/
- SHORT FAR PASCAL LibMain
- (HANDLE hModule, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
- {
- hInstance = hModule;
- wDataSeg;
- wHeapSize;
- lpszCmdLine;
-
- if(wHeapSize > 0)
- UnlockData(0);
-
- return TRUE;
- }
- /*-------------------------------------------------------------------------*
- *
- * Function: DLL ExitFuntion
- *
- * Parameter: nSystemExit - SHORT
- *
- * Return: SHORT
- nSystemExit - WEP_FREE_DLL
- * WEP_SYSTEM_EXIT
-
- * Comment:
- *-------------------------------------------------------------------------*/
- SHORT DLLAPI WEP (SHORT nSystemExit)
- {
- nSystemExit;
-
- return TRUE;
- }
- /*-------------------------------------------------------------------------*
- *
- * Function: TestFunction implements a simple calculation logic.
- *
- * Parameter: hInBuf - HLSTR
- *
- * Return: SHORT
- *
- * Comment:
- *-------------------------------------------------------------------------*/
- SHORT DLLAPI VBDllTestProc (HLSTR hInBuf)
- {
- static INPARAM Param;
-
- SHORT nRet;
- USHORT nSize;
- LPSTR lpBuf;
- HGLOBAL hBuf;
-
- nRet = FALSE;
-
- // Get the size of inputbuffer and look if its equal to the
- // size of the structure. If not give up.
- nSize = VBGetHlstrLen(hInBuf);
- if(nSize != sizeof(INPARAM))
- return ERR_STRUCTSIZE;
- // Allocate a global buffer and copy the VBString into it.
- hBuf = GlobalAlloc(GPTR, nSize + 1);
- lpBuf = (LPSTR)GlobalLock(hBuf);
- VBGetHlstr(hInBuf, (LPVOID)lpBuf, nSize);
- // Now copy the different peaces into the structure
- GetData(lpBuf, (LPINPARAM) &Param);
- /*-------------------------------------------------------------------------*
- From now on you can use the structure to easely manipulate the data
- like you would in a normal C program.
- *-------------------------------------------------------------------------*/
- switch(Param.nTyp)
- {
- case 0:
- Param.dblResult = Param.dblZ1 + Param.dblZ2;
- break;
- case 1:
- Param.dblResult = Param.dblZ1 - Param.dblZ2;
- break;
- case 2:
- Param.dblResult = Param.dblZ1 * Param.dblZ2;
- break;
- case 3:
- Param.dblResult = Param.dblZ1 / Param.dblZ2;
- break;
- }
- /*-------------------------------------------------------------------------*
- When your ready with this you can put the structure data back to VB
- *-------------------------------------------------------------------------*/
- // First back to the global buffer
- SetData(lpBuf, (LPINPARAM) &Param);
- // Then back to VBString
- VBSetHlstr(&hInBuf, lpBuf, nSize);
- // Just some cleanup and we are ready.
- GlobalUnlock(hBuf);
- GlobalFree(hBuf);
- return nRet;
-
- }
- /*-------------------------------------------------------------------------*
- *
- * Function: Copies the data from the inputbuffer (VBString)
- to a structure.
- *
- * Parameter: lpBuf - LPSTR
- * lpParam - LPINPARAM
- * Return:
- *
- * Comment:
- *-------------------------------------------------------------------------*/
- VOID FAR PASCAL GetData (LPSTR lpBuf, LPINPARAM lpParam)
- {
- LONG nSize,
- nPos;
-
- nSize = 0;
- nPos = 0;
- nSize = sizeof(lpParam->nTyp);
- hmemcpy(&(lpParam->nTyp), (lpBuf + nPos), nSize);
- nPos = nPos + nSize;
- nSize = sizeof(lpParam->dblZ1);
- hmemcpy(&(lpParam->dblZ1), (lpBuf + nPos), nSize);
- nPos = nPos + nSize;
- nSize = sizeof(lpParam->dblZ2);
- hmemcpy(&(lpParam->dblZ2), (lpBuf + nPos), nSize);
- nPos = nPos + nSize;
- nSize = sizeof(lpParam->dblResult);
- hmemcpy(&(lpParam->dblResult), (lpBuf + nPos), nSize);
-
- }
- /*-------------------------------------------------------------------------*
- *
- * Function: Copies the modified data back to the inputbuffer.
- *
- * Parameter: lpBuf - LPSTR
- * lpParam - LPINPARAM
- *
- * Return:
- *
- * Comment:
- *-------------------------------------------------------------------------*/
- VOID FAR PASCAL SetData (LPSTR lpBuf, LPINPARAM lpParam)
- {
- LONG nSize = 0,
- nPos = 0;
-
- nSize = 0;
- nPos = 0;
- nSize = sizeof(lpParam->nTyp);
- hmemcpy((lpBuf + nPos), &(lpParam->nTyp), nSize);
- nPos = nPos + nSize;
- nSize = sizeof(lpParam->dblZ1);
- hmemcpy((lpBuf + nPos), &(lpParam->dblZ1), nSize);
- nPos = nPos + nSize;
- nSize = sizeof(lpParam->dblZ2);
- hmemcpy((lpBuf + nPos), &(lpParam->dblZ2), nSize);
- nPos = nPos + nSize;
- nSize = sizeof(lpParam->dblResult);
- hmemcpy((lpBuf + nPos), &(lpParam->dblResult), nSize);
-
- }
-