Jump to content

CoCreateInstance


vbdavie

Recommended Posts

I have some code that LOCKS UP the DLL when running is ISAPI mode. Here's my code....

 

Function CreateShortCut_Lnk(RealFile,Parms,ShortCutName,LpszDescStr:AnsiString):Boolean;
Var
    psl:IShellLink;
    ppf:IPersistFile;
    HRes:HRESULT;
    wsz:PWideChar; {Max_Path}
    LpszParms,LpszPathObj,LpszPathLink,LpszDesc:Array[0..Max_Path]
{$IFDEF VER120}
        Of Char;
{$ELSE}
        Of WideChar;
{$ENDIF}
Begin
TranslateForVista(ShortCutName);
StrPCopy(LpszPathObj,RealFile);
StrPCopy(LpszPathLink,ShortCutName);
StrPCopy(LpszDesc,LpszDescStr);
StrPCopy(LpszParms,Parms);
{
CoInitialize(Nil);
}
GetMem(Wsz,Max_Path*SizeOf(WideChar));
Hres:=CoCreateInstance(CLSID_ShellLink,
                       Nil,
                       CLSCTX_INPROC_SERVER,
{$IFDEF VER120}
                       IID_IShellLinkA,
{$ELSE}
                       IID_IShellLinkW,
{$ENDIF}
                       psl);
If (SUCCEEDED(Hres)) Then
   Begin
   psl.SetPath(lpszPathObj);            -      Seems to lock up on this line
   psl.SetDescription(lpszDesc);
   psl.SetArguments(lpszParms);
   Hres:=psl.QueryInterface(IPersistFile,ppf);
   if (SUCCEEDED(Hres)) Then
      Begin
      MultiByteToWideChar(CP_ACP,
                          0,
                          PAnsiChar(AnsiString(lpszPathLink)),
                          -1,
                          wsz,
                          MAX_PATH);
      Hres:=ppf.Save(wsz, TRUE);
      End;
   End;
FreeMem(Wsz,Max_Path*SizeOf(WideChar));
{
CoUnInitialize;
}
Result:=SUCCEEDED(Hres);
End;

 

This basically allows me to create a shortcut file easily. I call it like this....

 

CreateShortCut_Lnk('TestShortCut.txt','',ExtractFilePath(ApplicationDLLName)+'TestShortCut.lnk','You can delete this file');

 

This allows me to create a test shortcut file that points to "TestShortCut.TXT".

 

When done during the initialization phase of a normal VCL program, it fixes a bug that VISTA has with memory leaks. Anyway, during the DLL ASAPI mode, when it hits the RED line, it basically messes up the uniGUI system. It make it lock up and stop servicing the other apps in the application pool. If I run the uniGUI in EXE mode, then it works fine. It's just in DLL mode that it hangs. What's wierd is that when I take out the call to CreateShortCut_Lnk in my INITIALIZATION code of my unit, and INSTEAD put the code on a buttonClick event, IT WORKS FINE and creates the shortcut and doesn't lock up :)

 

I think this is some sort of COM stuff, so maybe that's the deal.

HINT: If I DO NOT put it in the initialization section of my unit, and simply call it from a button click, then it WORKS. Very wierd.
 

Yet ANOTHER difference between the EXE mode and the DLL mode :)

 

Any ideas why the lock up?

 

Davie

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...