views:

961

answers:

3

Hi guys,

On Delphi 2009, on a new VCL project:

procedure TForm1.FormCreate(Sender: TObject);
var
    Handle: THandle;
begin
    Handle := loadlibrary('oci.dll');
    if Handle <> 0 then
    begin
        try
         ShowMessage('Success');
        finally
            FreeLibrary(Handle);
        end;
    end
    else
        ShowMessage('Fail');
end;

If i run the Exe from the IDE, it fails, if i run the EXE from the directory, just on double clicking on it it's a success !!??

Please tell me what I'm missing.

Thanks, Fred

EDIT: Launching the EXE via the IDE works with Delphi 7 !! WTf is the problem with D2009 ??

+2  A: 

If the dependent DLL is in the same directory... make sure your startup directory is it.

This happens to me all the time in Visual Studio too...

Thanks, but the dll is int the system path, not on the same directory as the exe
Fred
+4  A: 

The usual problem with LoadLibrary failing is that the required DLL is not in the DLL search path. It's possible that D2009 is not searching the same folders for some reason.

To make sure, you should get more details of the error, using something like...

ShowMessage(SysErrorMessage(GetLastError));

Try (even temporarily) placing "oci.dll" in the same directory as you project's .EXE, and try again.

Here are some things to check:

  • Which directory is oci.dll located in?
  • Is that directory included in the "PATH" environment variable? If not, try that.
  • Have you maybe set an OVERRIDE for PATH in Delphi Tools/Options/Env variables screen?
Roddy
Fred
@Fred. Understood - but moving (or copying) the dll just temporarily will confirm that this is the problem. THEN we can look for the solution...
Roddy
Ok, i moved the oci.dll in the same directory and it worked
Fred
GetLastError return code is 87 that seems to be "incorrect parameter"
Fred
My bad, error code is 126
Fred
ERROR_MOD_NOT_FOUND1260x7E The specified module could not be found.
Roddy
yep, but this error not happens white delphi 7 ide or running by double clicking on it, why only in that case ?
Fred
Ok problem solved. I checked environnement variables and the system path defined is not the same as the one as we can see by typing path in the console...Maybe D2009 took back old settings from my previously installed D2005..A GREAT THANKS FOR YOUR HELP !
Fred
Juste a note for those who run into the same problem: to get Delphi path environment var get in sync with global env var, just rename HCU/Software/CodeGear/BDS/6.0/Environment Variables/. Following this, Delphi will take the same Path as the system rather than the registry stored value.
Fred
That path is freely editable, for what it's worth - Project Options | Debugger | Environment Block. You can delete overrides directly in there, rather than messing around with the registry.
Barry Kelly
A: 

I would suspect that the stand alone call works because the DLL happens to be in the current directory.

And, when started from the IDE, the current directory differs from the EXE directory.

Did you try to use the full path to the DLL (i.e., something like path from ParamStr(0) plus the DLL name)?

sleepy012