Internet Explorer BHO -- Problem writing to file

G

giladap

I am trying to create a BHO using C#. I currently have it successfully
loading into IE (I've checked with "Manage Add-ons"), but nothing else
seems to be working. I get no errors that I can see.

I have the BHO trying to write to a text file. It is creating the text
file ("log.txt") when IE loads, but nothing is being written to it. What
might I be missing?

Below are the relevant snippets code. First, my declaration for the COM
interface "IObjectWithSite":


// The interface

[
ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC5971A5-C2D2-4442-9778-E924303A3399")
]
public interface IObjectWithSite
{
[PreserveSig]
int SetSite ([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite (ref Guid guid, out IntPtr ppvSite);
}


I've instantiated the object as follows:


// The object

[
ComVisible(true),
Guid("B5309629-6A3B-4e80-9C74-8A18F43E059A"),
ClassInterface(ClassInterfaceType.None)
]
public class BHO : IObjectWithSite
{


And the constructor is below. Note that "log.txt" is being created when
the constructor runs, but the very next line of code tries to write to
the file--and nothing occurs.


public BHO()
{
redirPagePath = typeof(DnsFilter).Assembly.Location;
redirPagePath = Path.Combine(Path.GetDirectoryName(redirPagePath),
"html\\blocked.html");

logFile = new StreamWriter(
Path.Combine(Path.GetDirectoryName(redirPagePath), "log.txt" ),false);
logFile.WriteLine(" Log Initialized");

}


What might I be missing? Any help would be appreciated. Thanks,

J.A.
 
A

Arne Janning

Hi giladap!

// The interface

[
ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC5971A5-C2D2-4442-9778-E924303A3399")
]
public interface IObjectWithSite
{
[PreserveSig]
int SetSite ([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite (ref Guid guid, out IntPtr ppvSite);
}

The Guid must be exactly "FC4801A3-2BA9-11CF-A229-00AA003D7352", otherwise
it will not work:

using System;
using System.Runtime.InteropServices;
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
public interface IObjectWithSite
{
[PreserveSig]
int SetSite ([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite (ref Guid guid, out IntPtr ppvSite);
}

Cheers

Arne Janning
 
G

gilad

Arne said:
Hi giladap!
The Guid must be exactly "FC4801A3-2BA9-11CF-A229-00AA003D7352", otherwise
it will not work:

using System;
using System.Runtime.InteropServices;
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
public interface IObjectWithSite
{
[PreserveSig]
int SetSite ([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite (ref Guid guid, out IntPtr ppvSite);
}

Thanks Arne, that worked. I missed the implication in the article I was
reading and thought I had to generate a unique GUID for both my class
and the interface.

My assumption is that because GUIDs are used to create "strong names",
the IObjectWithSite interface has already been given this one. Am I
correct in thinking this?

Thanks again,

gilad
 
A

Arne Janning

Hi gilad!

My assumption is that because GUIDs are used to create "strong names", the
IObjectWithSite interface has already been given this one. Am I correct in
thinking this?

Yes, you're right.

Cheers

Arne Janning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top