How to import COM interface - IPersistFile?

P

Plamen

Hi ,
I need to change the ToolTip in Windows explorer that is shown when the
cursor is over a file. Here is how it should work: you create a class that
imports IPersistFile and IQueryInfo. After adding some registry values when
you move the cursor over some file in Win Explorer, the shell will create
object from your class and will call IPersistFile::Load. Well here is the
problem - in my situation it is not called. The only method that is called
from IPersistFile interface is IPersistFile::IsDirty(). Then when IQueryInfo::
GetInfoTip is called I don't have the name of the file that is currently
under the cursor. Here is the code that I use to test. I bound it to txt
files only.
Do I import the IPersistFile interface properly?

namespace ShellExt
{
[ComImport(), ComVisible(true), InterfaceType(ComInterfaceType.
InterfaceIsIUnknown), GuidAttribute("0000010b-0000-0000-C000-000000000046")]
public interface IPersistFile
{
[PreserveSig()]
void Load(string pszFileName, int dwMode);

[PreserveSig()]
void GetClassID(out Guid pClassID);

[PreserveSig()]
void IsDirty();

[PreserveSig()]
void Save(String pszFileName, bool fRemember);

[PreserveSig()]
void SaveCompleted(String pszFileName);

[PreserveSig()]
void GetCurFile(out String ppszFileName);
}

[ComImport(), ComVisible(true), InterfaceType(ComInterfaceType.
InterfaceIsIUnknown), GuidAttribute("00021500-0000-0000-c000-000000000046")]
public interface IQueryInfo
{
[PreserveSig()]
void GetInfoTip(int flags, ref String toolTip);

[PreserveSig()]
void GetInfoFlags(ref long flags);
}
}

namespace ShellExt
{
/// <summary>
/// Summary description for ToolTipExtension.
/// </summary>
///
[Guid("6156C6FC-4DD9-4f82-8200-0446DABB7F35"), ComVisible(true)]
public class ToolTipExtension : IPersistFile, IQueryInfo
{
const string guid = "{6156C6FC-4DD9-4f82-8200-0446DABB7F35}";
const int E_NOTIMPL = unchecked((int)0x80004001);

public IPersistFile persistFile = null;
string fileName = string.Empty;

[PreserveSig()]
void IQueryInfo.GetInfoTip(int flags, ref string toolTip)
{
if (fileName == String.Empty)
toolTip = "Not working properly!";
else
toolTip = fileName;
}

[PreserveSig()]
void IQueryInfo.GetInfoFlags(ref long flags)
{
}

#region IPersistFile Members

[PreserveSig()]
void IPersistFile.Load(string pszFileName, int dwMode)
{
this.fileName = pszFileName.ToString();
}

[PreserveSig()]
void IPersistFile.GetClassID(out Guid pClassID)
{
pClassID = new Guid("6156C6FC-4DD9-4f82-8200-0446DABB7F35");
}

[PreserveSig()]
void IPersistFile.IsDirty()
{
}

[PreserveSig()]
void IPersistFile.Save(string pszFileName, bool fRemember)
{
}

[PreserveSig()]
void IPersistFile.SaveCompleted(string pszFileName)
{
}

[PreserveSig()]
void IPersistFile.GetCurFile(out string ppszFileName)
{
ppszFileName = null;
}

#endregion

#region Registration
[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]
static void RegisterServer(String str1)
{
try
{
RegistryKey root;
RegistryKey rk;

root = Registry.CurrentUser;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\
Explorer", true);
rk.SetValue("DesktopProcess", 1);
rk.Close();

// For Winnt set me as an approved shellex
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Shell
Extensions\\Approved", true);
rk.SetValue(guid, "ShellToolTip shell extension");
rk.Close();

// Set "Folder\\shellex\\ContextMenuHandlers\\ShellCmd" regkey to my guid
root = Registry.ClassesRoot;
rk = root.CreateSubKey(".txt\\shellex\\{00021500-0000-0000-C000-
000000000046}");
rk.SetValue("", guid);
rk.Close();
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
}

[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(String str1)
{
try
{
RegistryKey root;
RegistryKey rk;

// Remove ShellExtenstions registration
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Shell
Extensions\\Approved", true);
rk.DeleteValue(guid);
rk.Close();

// Delete ShellCmd regkey
root = Registry.ClassesRoot;
root.DeleteSubKey(".txt\\shellex\\{00021500-0000-0000-C000-000000000046}")
;
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
}
#endregion
}
}
 
G

Guest

Use www.pinvoke.net to check params of export interfaces

and look there
http://groups.google.com/group/micr...tnet+GetInfoTip&rnum=1&hl=en#6fb8557330d81fc7

where some issue was described.
Hi ,
I need to change the ToolTip in Windows explorer that is shown when the
cursor is over a file. Here is how it should work: you create a class that
imports IPersistFile and IQueryInfo. After adding some registry values when
you move the cursor over some file in Win Explorer, the shell will create
object from your class and will call IPersistFile::Load. Well here is the
problem - in my situation it is not called. The only method that is called
from IPersistFile interface is IPersistFile::IsDirty(). Then when IQueryInfo::
GetInfoTip is called I don't have the name of the file that is currently
under the cursor. Here is the code that I use to test. I bound it to txt
files only.
Do I import the IPersistFile interface properly?

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

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

Top