2012-03-16 86 views
2

我試圖訪問使用C#創建並註冊的COM對象,但沒有任何成功。訪問InnoSetup中的C#COM對象時出現異常

; Script generated by the Inno Setup Script Wizard. 
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! 

[Setup] 
AppName=CoolCOM 
AppVerName=CoolCOM 1.0 
CreateAppDir=no 
DisableProgramGroupPage=yes 
DefaultGroupName=CoolCOM 
UninstallDisplayIcon={app}\CoolCOM.exe 
OutputDir=userdocs:Inno Setup Examples Output 

[Files] 
Source : "UsingCOM.dll";DestDir: "{app}" 

[Code] 

const 
    CLSID_ShellLink = '{51E1EF73-0A8F-440a-B68F-614A83B515DB}'; 


procedure AboutButtonOnClick(Sender: TObject); 
var Form : TSetupForm; 
    OKButton,CancelButton : TNewButton; 
    FormCaption : TLabel; 
    Obj: Variant; 
begin 

{ Create the main ShellLink COM Automation object } 
    Obj := CreateOleObject('UsingCOM.CUsingCom'); 

try 
    Form := CreateCustomForm(); 
    Form.Clientwidth := 400; 
    Form.ClientHeight := 300; 
    Form.Caption := 'VATSAG Inc.'; 
    Form.Color := clGray; 
    Form.CenterInsideControl(WizardForm, False); 

    OKButton := TNewButton.Create(Form); 
    OKButton.Caption := '&OK'; 
    OKButton.Parent := Form; 
    OKButton.Top := Form.ClientHeight - ScaleY(25); 
    OKButton.Left := Form.ClientWidth - ScaleX(200); 
    OKButton.ModalResult := mrOk; 

    CancelButton := TNewButton.Create(Form); 
    CancelButton.Caption := '&Cancel'; 
    CancelButton.Parent := Form; 
    CancelButton.ModalResult := mrCancel; 
    CancelButton.Top := OKButton.Top; 
    CancelButton.Left := Form.ClientWidth - ScaleX(100); 

    FormCaption := TLabel.Create(Form); 
    FormCaption.Caption := Obj.GetCustomerName(); 
    FormCaption.Left := ScaleY(20); 
    FormCaption.Top := ScaleY(10); 
    FormCaption.Width := 200; 
    FormCaption.Height := 20; 
    FormCaption.Parent := Form; 
    FormCaption.WordWrap := True; 
    FormCaption.Font.Size := 12; 
    FormCaption.Font.Color := clWhite; 
    FormCaption.Font.Style := [fsBold]; 

    Form.ActiveControl := OKButton; 

    if Form.ShowModal = mrOk then begin 
    MsgBox('So you agree with me :)', mbInformation, mrOk); 
    end 
    else begin 
    MsgBox('Do you have a problem with me 8)', mbInformation, mrOk); 
    end; 

finally 
    Form.Free(); 
end; 

end; // EO AboutButtonOnClick 

procedure CreateAboutButtonAndURLLabel(ParentForm: TSetupForm; CancelButton: TNewButton); 
var 
    AboutButton: TNewButton; 
    URLLabel: TNewStaticText; 
begin 
    AboutButton := TNewButton.Create(ParentForm); 
    AboutButton.Left := ParentForm.ClientWidth - CancelButton.Left - CancelButton.Width; 
    AboutButton.Top := CancelButton.Top; 
    AboutButton.Width := CancelButton.Width; 
    AboutButton.Height := CancelButton.Height; 
    AboutButton.Caption := '&About...'; 
    AboutButton.OnClick := @AboutButtonOnClick; 
    AboutButton.Parent := ParentForm; 

end; 

procedure InitializeWizard(); 
var 
    Left, LeftInc, Top, TopInc: Integer; 
begin 
    Left := WizardForm.WelcomeLabel2.Left; 
    LeftInc := (WizardForm.CancelButton.Width*3)/2 + ScaleX(8); 
    TopInc := WizardForm.CancelButton.Height + ScaleY(8); 
    Top := WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height - 4*TopInc; 

    CreateAboutButtonAndURLLabel(WizardForm, WizardForm.CancelButton); 

end; 

其中obj.GetCustomerName()是公開的COM方法。 UsingCOM是命名空間和CUsingCom是類的名稱

任何人都可以指出我在哪裏搖搖欲墜?

+0

你有什麼豁免?同時創建COM對象或調用Obj.GetCustomerName? COM是否已經註冊(您需要先註冊UsingCOM.dll才能使用它)? – kobik 2012-03-16 17:16:37

+0

你能引用TLB而不是DLL嗎? – code4life 2012-03-16 19:24:01

回答

3

您需要第一註冊COM DLL 之前,您可以創建並使用它。 您可能需要將dll提取到目標,然後在致電CreateAboutButtonAndURLLabel之前致電RegisterServer

當使用[Files]部分時,您需要添加regserver屬性來註冊COM服務器,但是這會在您的安裝過程中太晚地複製和註冊dll。

+0

正如我所提到的,我已經註冊了DLL ...但我會嘗試添加regserver attr選項 – 2012-03-16 18:33:52

+0

對不起,我誤解了你的問題。我仍然無法理解您的設置過程。什麼會發生在一臺沒有註冊DLL的機器上(使用regsvr32)?在創建嚮導窗口之後完成文件複製過程...無論如何,嘗試從inno以外的其他應用程序加載COM dll以驗證其是否正確註冊。 – kobik 2012-03-16 22:18:25