|
Hi,
I'am new to this group so I would like to say "Hello" everyone
and here is my problem:
I'm writing a simple application (code is at the end of this message) witch
would list all mails from a directory from Outlook Express. I have:
- OE 6.0
- .NET Framework 1.1
- interface definition language for OE 6.0 - msoeapi.idl
- registered inetcomm.dll
When I try to get properties of messages something throws an error: "Object
reference not set to an instance of an object." (but only processing
particular messages - not all).
Here's a piece of code that reads properties of messages:
IStoreFolder isf = null;
HENUMSTORE hEnum = new HENUMSTORE();
MESSAGEPROPS pProps = new MESSAGEPROPS();
int wynik = 0;
try
{
ist.OpenFolder (90877, 0, out isf);
FOLDERPROPS p = new FOLDERPROPS();
p.cbSize = (System.UInt32)System.Runtime.InteropServices.Mars hal.SizeOf(p);
isf.GetFolderProps(0, ref p);
MessageBox.Show(p.szName);
textBox1.Clear();
pProps.cbSize =
(System.UInt32)System.Runtime.InteropServices.Mars hal.SizeOf(pProps);
isf.GetFirstMessage(0, 0, MESSAGEID_FIRST, ref pProps, ref hEnum);
for (int i = 0; i < p.cMessage; i++)
{
wynik = isf.GetNextMessage (hEnum, 0, ref pProps);
textBox1.AppendText(pProps.pszSubject + "(" + pProps.dwMessageId + ")" +
"\r\n\r\n");
isf.FreeMessageProps(ref pProps);
}
}
catch (Exception ex)
{
MessageBox.Show ("Nie udalo sie pobrac maili z katalogu: "+ex.Message+ "\n"+
ex.StackTrace);
}
}
which is enclosed within a procedure, before of course I initialize
apropriate environment:
string classKey = "{E70C92A9-4BFD-11d1-8A95-00C04FB951F3}";
Type objType = Type.GetTypeFromCLSID(new Guid(classKey));
ist = (IStoreNamespace) Activator.CreateInstance(objType);
ist.Initialize(0, 0);
When I try to do some brute force (enclose section that reads properties
within try-catch and igonre errors) whole program hangs up (no response) -
some investigation shows that after ignoring first first error (Object
reference not set to an instance of an object.") program during reading
properties of the next message thorws an exception:
System.ExecutionEngineException - which as I read in msdn is very, very bad.
When I switched to fast reading (setting MSGPROPS_FAST instead of 0) in
functions going through an enumeration (getFirstMessage i getNextMessage),
everything is fine (means all mails have been read), but using
getMessageProps after that ends up with the same exceptions as I mentioned
above. So I thought it is a problem in a construction of structure storing
properties of message which I had to map in c#, but after running a program
in a debugger I saw that the whole structure is filled correctly (all
entries within a structure has right values) and I started to do many
different things with that structure (including declaring every pointer (I
didn't need) as System.UInt32), with no effect - always the same
exceptions - but only with particular messages. I also tried to compare
messages cousing exceptions with those that are read correctly, but I
couldn't find any tip.
I have code in C++, operating through the same inferfaces (from
msoeapi.idl), which works fine - so interfaces works - the last think I'm
thinking about is an error within an .net framework, and before posting it
do microsoft I want to try everything. So far I haven't tried .net framework
2.0 - I want to know why it's not workinkg with 1.1
I'm running out of ideas, maybe somebody has any suggestions ? - would be
thankful.
PS. sorry for my poor english and that messages in messagebox are in polish
Slawomir Nasiadka
Code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace outlook_express_03
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
[StructLayout(LayoutKind.Sequential)]
public struct HENUMSTORE
{
public System.UInt32 unused;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct FOLDERPROPS /*: ValueType*/
{
public System.UInt32 cbSize;
public System.UInt32 dwFolderId;
public System.Int32 cSubFolders;
public SPECIALFOLDER sfType;
public System.UInt32 cUnread;
public System.UInt32 cMessage;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public String szName;
}
public enum SPECIALFOLDER : int
{
FOLDER_NOTSPECIAL = -1,
FOLDER_INBOX = FOLDER_NOTSPECIAL + 1,
FOLDER_OUTBOX = FOLDER_INBOX + 1,
FOLDER_SENT = FOLDER_OUTBOX + 1,
FOLDER_DELETED = FOLDER_SENT + 1,
FOLDER_DRAFT = FOLDER_DELETED + 1,
FOLDER_MAX = FOLDER_DRAFT + 1
}
public enum IMSGPRIORITY : int
{
IMSG_PRI_NORMAL = 2,
IMSG_PRI_LOW = 1,
IMSG_PRI_HIGH = 3
}
[ComImport,
Guid("E70C92AA-4BFD-11d1-8A95-00C04FB951F3"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
]
public interface IStoreNamespace
{
int Initialize([In] int a, [In] int b);
int GetDirectory([Out(), MarshalAs(UnmanagedType.LPStr)]
System.Text.StringBuilder pszPath, [In] System.Int32 cchMaxPath);
int OpenSpecialFolder (SPECIALFOLDER sfType, System.UInt32 dwReserved,
[MarshalAs(UnmanagedType.Interface)] out IStoreFolder ppFolder);
int OpenFolder(System.UInt32 storeFolderID, System.UInt32 dwReserved,
[MarshalAs(UnmanagedType.Interface)] out IStoreFolder ppFolder);
int CreateFolder (System.UInt32 storeFolderID, System.String pszName,
System.UInt32 dwReserved, out System.UInt32 pdwFolderID);
int RenameFolder ([In] System.UInt32 storeFolderID, [In] System.UInt32
Reserved, [In] System.String NewName);
int MoveFolder (int param1, int param2, int param3);
int DeleteFolder (int param1, int param2);
int GetFolderProps (System.UInt32 storeFolderID, System.UInt32 dwReserved,
ref FOLDERPROPS props);
int CopyMoveMessages (int p1, int p2, int p3, int p4, int p5, int p6);
int RegisterNotification (int p1, int p2);
int UnregisterNotification (int p1, int p2);
int CompactAll (int p1);
int GetFirstSubFolder(System.UInt32 storeFolderID, ref FOLDERPROPS props,
ref HENUMSTORE phEnum);
int GetNextSubFolder (HENUMSTORE hEnum, ref FOLDERPROPS props);
int GetSubFolderClose (HENUMSTORE hEnum);
}
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME/* : System.ValueType */
{
public System.UInt32 dwHighDateTime;
public System.UInt32 dwLowDateTime;
}
[StructLayout(LayoutKind.Sequential)]
public struct MESSAGEPROPS
{
public System.UInt32 cbSize;
public System.UInt32 dwReserved;
public System.UInt32 dwMessageId;
public System.UInt32 dwLanguage;
public System.UInt32 dwState;
public System.UInt32 cbMessage;
public /*IMSGPRIORITY*/System.UInt32 priority;
public FILETIME ftRecived;
public FILETIME ftSent;
public System.String pszSubject;
public System.String pszDisplayTo;
public System.String pszDisplayFrom;
public System.String pszNormalSubject;
public System.UInt32 dwFlags;
[MarshalAs(UnmanagedType.IUnknown)]
public IStream pstmOffsetTable;
}
[ComImport,
Guid("E70C92AC-4BFD-11d1-8A95-00C04FB951F3"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
]
public interface IStoreFolder
{
int GetFolderProps (System.UInt32 dwReserved, ref FOLDERPROPS pProps);
int GetMessageProps(System.UInt32 dwMessageId, System.UInt32 dwFlags, ref
MESSAGEPROPS pProps);
int FreeMessageProps(ref MESSAGEPROPS pProps);
int DeleteMessages(int param1, System.UInt32 dwReserved, int param2);
int SetLanguage(System.UInt32 dwLanguage, System.UInt32 dwReserved, int
param1);
int MarkMessagesAsRead(int param1, System.UInt32 dwReserved, int param2);
int SetFlags(int param1, System.UInt32 dwState, System.UInt32 dwStatemask,
int param4);
int OpenMessage(System.UInt32 dwMessageId, int param1, int param2);
int SaveMessage(int param1, int param2, System.UInt32 dwMsgFlags,int
param3);
int BatchLock(System.UInt32 dwReserved, int param1);
int BatchFlush(System.UInt32 dwReserved,int param1);
int BatchUnlock(System.UInt32 dwReserved, int param1);
int CreateStream(int param1, System.UInt32 dwReserved, int param2, int
param3);
int CommitStream(int param1, System.UInt32 dwFlags, System.UInt32
dwMsgFlags, int param2, int param3, int param4);
int RegisterNotification(System.UInt32 dwReserved, int param1);
int UnregisterNotification(System.UInt32 dwReserved, int param1);
int Compact(System.UInt32 dwReserved);
int GetFirstMessage(System.UInt32 dwFlags, System.UInt32 dwMsgFlags,
System.UInt32 dwMsgIdFirst, ref MESSAGEPROPS pProps, ref HENUMSTORE phEnum);
int GetNextMessage(HENUMSTORE hEnum, System.UInt32 dwFlags, ref MESSAGEPROPS
pProps); // When function returns S_OK, contains message properties
int GetMessageClose(HENUMSTORE hEnum);
}
[ComImport,
Guid("0C733A30-2A1C-11CE-ADE5-00AA0044773D"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
]
public interface ISequentialStream
{
int RemoteRead ( System.Byte pv , System.UInt32 cb , System.UInt32
pcbRead );
int RemoteWrite ( System.Byte pv , System.UInt32 cb , System.UInt32
pcbWritten );
}
[ComImport,
Guid("0000000C-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
]
public interface IStream /*: ISequentialStream*/
{
void Read([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] byte[]
pv, int cb, IntPtr pcbRead);
void Write([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] byte[] pv,
int cb, IntPtr pcbWritten);
void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition);
void SetSize(long libNewSize);
void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten);
void Commit(int grfCommitFlags);
void Revert();
void LockRegion(long libOffset, long cb, int dwLockType);
void UnlockRegion(long libOffset, long cb, int dwLockType);
void Stat(out STATSTG pstatstg, int grfStatFlag);
void Clone(out IStream ppstm);
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button7;
private IStoreNamespace ist;
const uint MESSAGEID_FIRST = 0xffffffff;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button7 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "inicjuj namespace";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(24, 152);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(152, 23);
this.button5.TabIndex = 4;
this.button5.Text = "pobierz wlasciwosci folderu";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(248, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(304, 232);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "textBox1";
//
// button7
//
this.button7.Location = new System.Drawing.Point(144, 88);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(96, 23);
this.button7.TabIndex = 7;
this.button7.Text = "maile z 6";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(576, 266);
this.Controls.Add(this.button7);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
string classKey = "{E70C92A9-4BFD-11d1-8A95-00C04FB951F3}";
Type objType = Type.GetTypeFromCLSID(new Guid(classKey));
ist = (IStoreNamespace) Activator.CreateInstance(objType);
ist.Initialize(0, 0);
}
private void button5_Click(object sender, System.EventArgs e)
{
try
{
FOLDERPROPS p = new FOLDERPROPS();
p.cbSize = (System.UInt32)System.Runtime.InteropServices.Mars hal.SizeOf(p);
ist.GetFolderProps(1, 0, ref p);
textBox1.Clear();
textBox1.AppendText("Nazwa katalogu: "+p.szName+"\r\n");
textBox1.AppendText("Numer katalogu: "+p.dwFolderId+"\r\n");
textBox1.AppendText("Typ katalogu: "+p.sfType+"\r\n");
textBox1.AppendText("Ilosc wiadomosci w tym folderze: "+p.cMessage+"\r\n");
textBox1.AppendText("Ilosc wiadomosci nieprzeczytanych: "+p.cUnread+"\r\n");
textBox1.AppendText("Liczba podkatalogow: "+p.cSubFolders+"\r\n");
FOLDERPROPS p1 = new FOLDERPROPS();
HENUMSTORE hEnum = new HENUMSTORE();
p1.cbSize =
(System.UInt32)System.Runtime.InteropServices.Mars hal.SizeOf(p1);
ist.GetFirstSubFolder(p.dwFolderId, ref p1, ref hEnum);
for (int i = 0; i < p.cSubFolders; i++)
{
textBox1.AppendText("\t"+p1.szName+" ("+p1.dwFolderId+")"+"\r\n");
ist.GetNextSubFolder(hEnum, ref p1);
}
ist.GetSubFolderClose (hEnum);
}
catch (Exception ex)
{
MessageBox.Show("Nie udalo sie pobrac wlasciwosci folderu: "+ex.Message+
"\n"+ ex.StackTrace);
}
}
private void button7_Click(object sender, System.EventArgs e)
{
IStoreFolder isf = null;
HENUMSTORE hEnum = new HENUMSTORE();
MESSAGEPROPS pProps = new MESSAGEPROPS();
int wynik = 0;
try
{
ist.OpenFolder (90877, 0, out isf); //90877 is a number of folder from which
mails will be listed (button5 lists all folders under the top folder with
their numbers)
FOLDERPROPS p = new FOLDERPROPS();
p.cbSize = (System.UInt32)System.Runtime.InteropServices.Mars hal.SizeOf(p);
isf.GetFolderProps(0, ref p);
MessageBox.Show(p.szName);
textBox1.Clear();
pProps.cbSize =
(System.UInt32)System.Runtime.InteropServices.Mars hal.SizeOf(pProps);
isf.GetFirstMessage(0, 0, MESSAGEID_FIRST, ref pProps, ref hEnum);
for (int i = 0; i < p.cMessage; i++)
{
wynik = isf.GetNextMessage (hEnum, 0, ref pProps);
textBox1.AppendText(pProps.pszSubject + "(" + pProps.dwMessageId + ")" +
"\r\n\r\n");
isf.FreeMessageProps(ref pProps);
}
}
catch (Exception ex)
{
MessageBox.Show ("Nie udalo sie pobrac maili z katalogu: "+ex.Message+ "\n"+
ex.StackTrace);
}
}
}
}
and a simple mail causes an exception:
subject: bleble
content: This is a message that couses an exception. |