zoukankan      html  css  js  c++  java
  • C++ calling a dll.

     C++ calling a dll.
    Ok lets say we have a function we want to call in C++. We would normally do this: 

    /* declare the function */ 
    int MyFunction(char*,int); 
    int main(void

       returnMyFunction("hello", 5); 


    /* our function */ 
    int MyFunction(char*,int

       /* do something */ 
       return 0; 

    ---------------------------------------------------------------------------------- 

    But in our case that same function is in a dll. Ok let's see how we do it. 
    Your dll is located on the C: drive for example and is named "MyDLL.dll". It 
    contains a function called "MyFunction". Create a function that calls the dll's function like so: 

    int CallMyDLL(void

        /* get handle to dll */ 
       HINSTANCE hGetProcIDDLL = LoadLibrary("C:\MyDLL.dll"); 

       /* get pointer to the function in the dll*/ 
       FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"MyFunction"); 

       /* 
          Define the Function in the DLL for reuse. This is just prototyping the dll's function. 
          A mock of it. Use "stdcall" for maximum compatibility. 
       */
     
       typedef int (__stdcall * pICFUNC)(char *, int); 

       pICFUNC MyFunction; 
       MyFunction = pICFUNC(lpfnGetProcessID); 

       /* The actual call to the function contained in the dll */ 
       int intMyReturnVal = MyFunction("hello", 5); 

       /* Release the Dll */ 
       FreeLibrary(hGetProcIDDLL); 

       /* The return val from the dll */ 
        returnintMyReturnVal; 


    You can just copy and paste the above into your project and change the prototyping to match your dll's function. 

    http://www.daniweb.com/software-development/cpp/threads/177816/c-calling-a-dll

    Hi i have a requirement where a cpp file should load a dll viz gzip.dll.then the cpp should call few methods in the dll.well im able to call the methods with no parameters but im unable to call the methods with parameters.if im trying to call methods with parameters i am getting memory exception in runtime.the program compiles very fine.i actually replicated the code in vb where a similair call has been made to the methods in gzip.dll for compression and decompression.but in cpp it doesnt work for calling these methods with the exact parameters.may be my code has a flaw.


    Please c the file.u can find the gzip.dll from C:WINNTsystem32gzip.dll

      1. #include<windows.h>
      2. #include<stdio.h>
      3. int main()
      4. {
      5. HINSTANCE hGetProcIDDLL =LoadLibrary("C:\WINNT\system32\gzip.dll");
      6. printf("dll loaded");
      7. /* get pointer to the function in the dll*/
      8. FARPROC lpfnGetProcessID =GetProcAddress(HMODULE (hGetProcIDDLL),"Decompress");
      9. FARPROC lpfnGetProcessID1 =GetProcAddress(HMODULE (hGetProcIDDLL),"InitDecompression");
      10. FARPROC lpfnGetProcessID2 =GetProcAddress(HMODULE (hGetProcIDDLL),"CreateDecompression");
      11. FARPROC lpfnGetProcessID3 =GetProcAddress(HMODULE (hGetProcIDDLL),"InitCompression");
      12. FARPROC lpfnGetProcessID4 =GetProcAddress(HMODULE (hGetProcIDDLL),"CopyMemory");
      13. // Call CopyMemory(arrDestination(destbeginPos), arrSource(srcbeginPos), length)
      14. // printf("Hello World long decompress! ");
      15. //printf(1);
      16. typedeflong(__stdcall * pICFUNC)(long, BYTE[],long,BYTE[],long,long,long);
      17. typedeflong(__stdcall * pICFUNC1)();
      18. typedeflong(__stdcall * pICFUNC2)(long,int);
      19. typedeflong(__stdcall * pICFUNC3)();
      20. typedeflong(__stdcall * pICFUNC4)(BYTE[],BYTE[],long);
      21. typedef UINT (CALLBACK*CopyMemory)(BYTE[],BYTE[],long);
      22. CopyMemory ptrCopyMemory;
      23. ptrCopyMemory =(CopyMemory)GetProcAddress(hGetProcIDDLL,"CopyMemory");
      24. typedef UINT (CALLBACK*InitDecompression)();
      25. InitDecompression ptrInitDecompression;
      26. ptrInitDecompression =(InitDecompression)GetProcAddress(hGetProcIDDLL,"InitDecompression");
      27. //ReturnVal = ptrLockWorkStation();
      28. ptrInitDecompression();
      29. printf("Hello World long decompress! ");
      30. pICFUNC MyFunction;
      31. MyFunction= pICFUNC(lpfnGetProcessID);
      32. pICFUNC1 MyFunction1;
      33. MyFunction1= pICFUNC1(lpfnGetProcessID1);
      34. pICFUNC2 MyFunction2;
      35. MyFunction2= pICFUNC2(lpfnGetProcessID2);
      36. pICFUNC3 MyFunction3;
      37. MyFunction3= pICFUNC3(lpfnGetProcessID3);
      38. pICFUNC4 MyFunction4;
      39. MyFunction4= pICFUNC4(lpfnGetProcessID4);
      40. //BYTE[] a=NewByteArray(jb);
      41. unsignedlong handle=0;long max=1024;long inu=0;long outu=0;int GZIP_LVL=1;
      42. BYTE *AB={(unsignedchar*)"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"};
      43. // int li= strlen( (char*)AB);
      44. int orglen=100;
      45. //printf("%l", li);
      46. BYTE *oB=new BYTE[43822];
      47. CopyMemory(AB,oB,234);
      48. printf("function defined and parameters ready"+orglen,"%l",orglen);
      49. printf("function defined and parameters ready");
      50. /* The actual call to the function contained in the dll */
      51. handle=MyFunction1();
      52. //handle=MyFunction4(AB,oB,inu);
      53. handle=MyFunction3();
      54. printf("InitDecompression & InitCompression");
      55. MyFunction2(handle,GZIP_LVL);//Call CreateDecompression(handle, GZIP_LVL);
      56. //handle=MyFunction2(handle,GZIP_LVL);
      57. printf("CreateDecompression");
      58. longintMyReturnVal=0;
      59. do
      60. {// int
      61. intMyReturnVal=MyFunction(handle, AB, orglen, oB, max, inu, outu);
      62. orglen = orglen - inu;
      63. }
      64. while(intMyReturnVal=0);
      65. /* Release the Dll */
      66. FreeLibrary(hGetProcIDDLL);
      67. printf("Hello World long decompress! ");
      68. /* The return val from the dll */
      69. returnintMyReturnVal;
      70. }
  • 相关阅读:
    C语言高速入口系列(七)
    数据结构:最小生成树--Prim算法
    poj2387-Til the Cows Come Home dijkstra获得水的问题
    iOS开展UI一片—简单的浏览器观看节目
    spark安装mysql与hive
    键入强力推进并解决强转
    华为u8800怎样root?
    用友ERP-U8最新破解(再次更新版本,附安装过程中的解决办法)
    Delphi 自带的那个 Hand 光标很难看?没关系,一行代码解决问题:
    阿里余额宝的来龙与去脉
  • 原文地址:https://www.cnblogs.com/gaoquanning/p/3465665.html
Copyright © 2011-2022 走看看