CallingADLLFromCVI.c
上传用户:zhuode
上传日期:2019-07-27
资源大小:198k
文件大小:4k
- #include <cvirte.h>
- #include <userint.h>
- #include <ansi_c.h>
- #include "CallingADLLFromCVI.h"
- //Include windows.h to call the DLL dynamically. windows.h contains LoadLibrary and GetProcAddress .
- #include <windows.h>
- //Include SimpleDLL.h to call the DLL statically. SimpleDLL.h contains the function prototype.
- #include "SimpleDLL.h"
- //For calling dynamically
- //Typedef the pointer to the exported function so you can call it easily later
- //The function returns an int and has two inputs, an int and a string
- typedef int (*MYPROC)(int, LPTSTR);
- static int panelHandle;
- int main (int argc, char *argv[])
- {
- if (InitCVIRTE (0, argv, 0) == 0)
- return -1; /* out of memory */
- if ((panelHandle = LoadPanel (0, "CallingADLLFromCVI.uir", PANEL)) < 0)
- return -1;
- DisplayPanel (panelHandle);
- RunUserInterface ();
- DiscardPanel (panelHandle);
- return 0;
- }
- int CVICALLBACK panelCB (int panel, int event, void *callbackData,
- int eventData1, int eventData2)
- {
- switch (event)
- {
- case EVENT_GOT_FOCUS:
- break;
- case EVENT_LOST_FOCUS:
- break;
- case EVENT_CLOSE:
- QuitUserInterface (0);
- break;
- }
- return 0;
- }
- int CVICALLBACK CallDLLStatically (int panel, int control, int event,
- void *callbackData, int eventData1, int eventData2)
- {
- int number; //Number to pass to DLL
- char string[50]; //String to pass to DLL
- int returnValue; //Number to store return value from DLL
- char message[50];
-
- switch (event)
- {
- case EVENT_COMMIT:
-
- //First get the inputs from the UI
- GetCtrlVal(panelHandle, PANEL_NUMERIC, &number);
- GetCtrlVal(panelHandle, PANEL_STRING, string);
-
- //Simply call the function like you would any other function.
- //The reason this works is because the function prototype is declared in SimpleDLL.h which is "#include"'ed above
- //and during build time, the linker will link with the import library SimpleDLL.lib to resolve the external reference
- returnValue = ShowMyNumberAndString(number, string);
-
- sprintf (message, "The function returned %d", returnValue);
- MessagePopup("Function Called Successfully", message);
-
- break;
- }
- return 0;
- }
- int CVICALLBACK CallDLLDynamically (int panel, int control, int event,
- void *callbackData, int eventData1, int eventData2)
- {
- int number; //Number to pass to DLL
- char string[50]; //String to pass to DLL
- int returnValue; //Number to store return value from DLL
- char message[50];
-
- HINSTANCE hinstLib; //Handle to the DLL
- MYPROC ProcAddress; //Pointer to the function
- BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
-
- switch (event)
- {
- case EVENT_COMMIT:
- //First get the inputs from the UI
- GetCtrlVal(panelHandle, PANEL_NUMERIC, &number);
- GetCtrlVal(panelHandle, PANEL_STRING, string);
-
- //Load the DLL and get a handle to it. LoadLibrary is part of the Windows SDK and is declared in windows.h
- //Calling LoadLibrary will cause the DLL's DLLMain function to execute
- hinstLib = LoadLibrary("SimpleDLL.dll");
-
- if (hinstLib != NULL)
- {
- //The the pointer to the exported function and typecast it so that we can easily call it
- //MYPROC is typedef'ed above
- //GetProcAddress is part of the Windows SDK and is declared in windows.h
- ProcAddress = (MYPROC) GetProcAddress(hinstLib, "ShowMyNumberAndString");
-
- if (fRunTimeLinkSuccess = (ProcAddress != NULL))
- {
- //Call the function using the function pointer
- returnValue = (ProcAddress)(number, string);
-
- sprintf (message, "The function returned %d", returnValue);
- MessagePopup("Function Called Successfully", message);
-
- //Free the DLL
- fFreeResult = FreeLibrary(hinstLib);
- }
- else
- {
- MessagePopup("Error", "Error: Could not get Address of Function");
- }
- }
- else
- {
- MessagePopup("Error", "Error: Could not Load Library (DLL)");
- }
-
- break;
- }
- return 0;
- }