Announcement

Collapse
No announcement yet.

Porting C++ code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Porting C++ code

    Hi All!

    What PB command should I use to call dll that expose only one function:
    Code:
    __declspec(dllexport) LPVOID WINAPI CreateInstance(char *DeviceName)
    {
    	char DevName[7];
    
    	strcpy(DevName, DeviceName);
    	strupr(DevName);
    	if(!strcmp(DevName, "IDRV"))
    	{
    		IDRV *pDrv = static_cast<IDRV *>(new ixDrv(hInstance));
    		return pDrv;
    	}
    	return NULL;
    }
    Code:
    *pDrv
    returns pointer to class members:
    Code:
    class ixDRV : public IDrv 
    {
    	virtual HANDLE WINAPI GetModuleHandle(void);
    	virtual bool WINAPI InitDevice(WORD VirtualSlot);
    	virtual bool WINAPI GetModuleName(char *ModuleName);
    	virtual bool WINAPI ReleaseLDevice(void);
    	....
    }
    May I use CALL DWORD xx USING syntax to call class members or it's impossible?


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

  • #2
    Any suggestions?


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

    Comment


    • #3
      The topic says "Porting C++ code" (ie, rewriting it in PowerBASIC) but the question seems to be about writing PowerBASIC code to <U>call</U> a C++ library. Can you be more specific about the approach you want to pursue please?

      What sort of library is it? The virtual function arrangement suggests that it is a COM library, in which case you might be able to use COM Automation.

      In any case, the primary function would translate to a DECLARE that goes something like this:
      Code:
      DECLARE CreateInstance LIB "dllname.dll" ALIAS "CreateInstance" (BYREF DeviceName AS ASCIIZ) AS DWORD
      The DWORD return value would then be a pointer to a table of pointers (usually called a virtual table or vtable), which most probably contains pointers to the other functions. In COM, the vtable pointers would be called "interface members", and the target of the pointers would be the member "methods" and "properties" exposed by the interface.

      If COM Automation is not going to work, then the hard way is to CALL DWORD to call these functions after obtaining the target pointer from the vtable.


      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        Lance,

        Thanks for answer.
        I put wrong topic name; should be a 'Calling C++ dll from PB'.Sorry.

        The library doesn't support COM automation,
        so I decided to use CALL DWORD command.
        The problem is to obtaining the target pointer from the vtable.
        Following C++ code call InitLDevice method:

        Code:
         ....
         hInstLib = LoadLibrary("iDevice.dll");
        //--- address of interface module 
         CreateInstIDRV = (CINST) GetProcAddress(hInstLib,         "CreateInstance");
         if(CreateInstIDRV == NULL) {printf("Error\n");exit(1);}
        //--- obtaining pointer to the interface
         pDrv = (IDRV*) CreateInstIDRV("IDRV");
         if(pDrv == NULL)
          {printf("Driver Interface module - Bad\n");exit(1);}
        //--- try to find device  
         if(!pDrv->InitLDevice(0)){ printf("InitLDevice()\n);}
        ....
        Hope, this piece of code will help.

        Thanks,
        Aslan.



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

        Comment

        Working...
        X