Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Delphi to C++ 2010

Status
Not open for further replies.

ik0

Programmer
Jun 2, 2010
14
MK
Can somebody "translate" me this code? The code is used to check whether the user uses internet while he is using the aplication.

Code:
procedure TForm1.Button1Click(Sender: TObject) ;

  function FuncAvail(_dllname, _funcname: string;
                     var _p: pointer): boolean;
  {return True if _funcname exists in _dllname}
  var _lib: tHandle;
  begin
   Result := false;
   if LoadLibrary(PChar(_dllname)) = 0 then exit;
   _lib := GetModuleHandle(PChar(_dllname)) ;
   if _lib <> 0 then begin
    _p := GetProcAddress(_lib, PChar(_funcname)) ;
    if _p <> NIL then Result := true;
   end;
  end;

  {
  Call SHELL32.DLL for Win < Win98
  otherwise call URL.dll
  }
  {button code:}
  var
   InetIsOffline : function(dwFlags: DWORD):
                   BOOL; stdcall;
  begin
   if FuncAvail('URL.DLL', 'InetIsOffline',
                @InetIsOffline) then
    if InetIsOffLine(0) = true
     then ShowMessage('Not connected')
     else ShowMessage('Connected!') ;
  end;
 
My Pascal is very rusty. I haven't really used it in almost 30 years and I'm doing this from memory so beware!

Code:
procedure TForm1.Button1Click(Sender: TObject) ;

  function FuncAvail(_dllname, _funcname: string;
                     var _p: pointer): boolean;
  {return True if _funcname exists in _dllname}
  var _lib: tHandle;
  begin
   Result := false;
   if LoadLibrary(PChar(_dllname)) = 0 then exit;
   _lib := GetModuleHandle(PChar(_dllname)) ;
   if _lib <> 0 then begin
    _p := GetProcAddress(_lib, PChar(_funcname)) ;
    if _p <> NIL then Result := true;
   end;
  end;
might end up something line this:
Code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  
}

boolean FuncAvail(string _dllname, string _funcname, *_p)
{
   // I'm not certain how this fits into the procedure above
  // DO NOT USE _variable name in C++ as they are usually reserved for compiler variables
  // Change the names of all _variables 
  
  //return True if _funcname exists in _dllname
  tHandle _lib;
  Result = false;
  if (LoadLibrary(PChar(_dllname)) == 0) exit;
  _lib = GetModuleHandle(PChar(_dllname)) ;
  if (_lib <> 0)
  {
     _p = GetProcAddress(_lib, PChar(_funcname)) ;
     if (_p <> NIL) Result = true;
   }
}

Notice that := becomes =, = becomes ==, var _lib: tHandle; becomes tHandle _lib;, and begin . . . end becomes { . . . }. Also, notice that in C++ you should not preface a variable with an underscore (_).


James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top