CallingADLLFromCVI.c
上传用户:zhuode
上传日期:2019-07-27
资源大小:198k
文件大小:4k
源码类别:

系统编程

开发平台:

LabView

  1. #include <cvirte.h>
  2. #include <userint.h>
  3. #include <ansi_c.h>
  4. #include "CallingADLLFromCVI.h"
  5. //Include windows.h to call the DLL dynamically. windows.h contains LoadLibrary and GetProcAddress .
  6. #include <windows.h>
  7. //Include SimpleDLL.h to call the DLL statically. SimpleDLL.h contains the function prototype.
  8. #include "SimpleDLL.h"
  9. //For calling dynamically
  10. //Typedef the pointer to the exported function so you can call it easily later
  11. //The function returns an int and has two inputs, an int and a string
  12. typedef int (*MYPROC)(int, LPTSTR);
  13. static int panelHandle;
  14. int main (int argc, char *argv[])
  15. {
  16. if (InitCVIRTE (0, argv, 0) == 0)
  17. return -1; /* out of memory */
  18. if ((panelHandle = LoadPanel (0, "CallingADLLFromCVI.uir", PANEL)) < 0)
  19. return -1;
  20. DisplayPanel (panelHandle);
  21. RunUserInterface ();
  22. DiscardPanel (panelHandle);
  23. return 0;
  24. }
  25. int CVICALLBACK panelCB (int panel, int event, void *callbackData,
  26. int eventData1, int eventData2)
  27. {
  28. switch (event)
  29. {
  30. case EVENT_GOT_FOCUS:
  31. break;
  32. case EVENT_LOST_FOCUS:
  33. break;
  34. case EVENT_CLOSE:
  35. QuitUserInterface (0);
  36. break;
  37. }
  38. return 0;
  39. }
  40. int CVICALLBACK CallDLLStatically (int panel, int control, int event,
  41. void *callbackData, int eventData1, int eventData2)
  42. {
  43. int number; //Number to pass to DLL
  44. char string[50]; //String to pass to DLL
  45. int returnValue; //Number to store return value from DLL
  46. char message[50];
  47. switch (event)
  48. {
  49. case EVENT_COMMIT:
  50. //First get the inputs from the UI
  51. GetCtrlVal(panelHandle, PANEL_NUMERIC, &number);
  52. GetCtrlVal(panelHandle, PANEL_STRING, string);
  53. //Simply call the function like you would any other function.
  54. //The reason this works is because the function prototype is declared in SimpleDLL.h which is "#include"'ed above
  55. //and during build time, the linker will link with the import library SimpleDLL.lib to resolve the external reference 
  56. returnValue = ShowMyNumberAndString(number, string);
  57. sprintf (message, "The function returned %d", returnValue);
  58. MessagePopup("Function Called Successfully", message);
  59. break;
  60. }
  61. return 0;
  62. }
  63. int CVICALLBACK CallDLLDynamically (int panel, int control, int event,
  64. void *callbackData, int eventData1, int eventData2)
  65. {
  66. int number; //Number to pass to DLL
  67. char string[50]; //String to pass to DLL
  68. int returnValue; //Number to store return value from DLL
  69. char message[50];
  70. HINSTANCE hinstLib; //Handle to the DLL
  71. MYPROC ProcAddress; //Pointer to the function
  72. BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
  73. switch (event)
  74. {
  75. case EVENT_COMMIT:
  76. //First get the inputs from the UI 
  77. GetCtrlVal(panelHandle, PANEL_NUMERIC, &number);
  78. GetCtrlVal(panelHandle, PANEL_STRING, string);
  79. //Load the DLL and get a handle to it. LoadLibrary is part of the Windows SDK and is declared in windows.h
  80. //Calling LoadLibrary will cause the DLL's DLLMain function to execute
  81. hinstLib = LoadLibrary("SimpleDLL.dll");
  82. if (hinstLib != NULL)
  83. {
  84. //The the pointer to the exported function and typecast it so that we can easily call it
  85. //MYPROC is typedef'ed above
  86. //GetProcAddress is part of the Windows SDK and is declared in windows.h
  87. ProcAddress = (MYPROC) GetProcAddress(hinstLib, "ShowMyNumberAndString");
  88. if (fRunTimeLinkSuccess = (ProcAddress != NULL))
  89. {
  90. //Call the function using the function pointer
  91. returnValue = (ProcAddress)(number, string);
  92. sprintf (message, "The function returned %d", returnValue);
  93. MessagePopup("Function Called Successfully", message);
  94. //Free the DLL
  95. fFreeResult = FreeLibrary(hinstLib);
  96. }
  97. else
  98. {
  99. MessagePopup("Error", "Error: Could not get Address of Function");
  100. }
  101. }
  102. else
  103. {
  104. MessagePopup("Error", "Error: Could not Load Library (DLL)");
  105. }
  106. break;
  107. }
  108. return 0;
  109. }