Faxing with C# using Fax Service Extended COM API - FAXCOMEXLib and C#

  • Thread starter Thread starter Patrick Kenney via AdminLife
  • Start date Start date
P

Patrick Kenney via AdminLife

this code works but I am having trouble with making it wait using AutoResetEvent[] or ManualResetEvent any suggestions with a code snippet of what to try?

Microsoft does not appear to have any C# samples of event handling with their fax service api using C# and the c++ and vb6 ones do not translate well to C#.

if I am missing something please advise...

thanks in advance.


namespace FaxDrill
{
public class FaxDrill
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);

public static AutoResetEvent[] autoEvents; //event array
public static int intAryCtr;

protected FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM FaxSvrEnum;
protected FAXCOMEXLib.FaxDocument objFaxDocument = new FAXCOMEXLib.FaxDocumentClass();
protected FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM objFaxSvrEnum;
protected FAXCOMEXLib.FaxServer objFaxServer;
protected static object JobID;

public static void Main()
{
try
{
SendFaxOrEmail();
}

catch (Exception ex)
{
MessageBox(0,ex.Message + "source: " + ex.Source + "stack trace: " + ex.StackTrace ,"error",0);
}

}

//event handlers
public static void objFaxServer_OnOutgoingJobAdded(FaxServer pFaxServer, string bstrJobId)
{
MessageBox(0,"JobID: " + bstrJobId + "Added to queue","csFax",0);
}

public static void objFaxServer_OnServerShutDown(FaxServer pFaxServer)
{
MessageBox(0,"Fax Server Shutting Down","csFax",0);
}

public static void objFaxServer_OnOutgoingJobChanged(FaxServer pFaxServer, string bstrJobId, FaxJobStatus pJobStatus)
{
if (pJobStatus.Status == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsCOMPLETED)
{
try
{
MessageBox(0,"Fax Job Completed","csFax",0);
autoEvents[intAryCtr].Set();
}

catch (Exception Ex)
{
MessageBox(0,Ex.Message,"thread error",0);
}
}

}//end onOutGoingJobChanged

public static void SendFaxOrEmail()
{
try
{
FaxDrill objFaxDrill = new FaxDrill();
string FileName;
string strSubString;

objFaxDrill.objFaxServer = new FAXCOMEXLib.FaxServer();
objFaxDrill.objFaxServer.Connect("");//empty string will default to local host.

objFaxDrill.objFaxSvrEnum = (FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM.fsetOUT_QUEUE | FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM.fsetFXSSVC_ENDED);

//Register this method to listen to fax status events.
objFaxDrill.objFaxServer.ListenToServerEvents(objFaxDrill.objFaxSvrEnum);
/*
Delegates.
To wire the events, create a delegate instance and add it to the event you want to raise.
E.G.: OnOutgoingJobAdded event.
*/
objFaxDrill.objFaxServer.OnOutgoingJobAdded +=new IFaxServerNotify_OnOutgoingJobAddedEventHandler(objFaxServer_OnOutgoingJobAdded);
objFaxDrill.objFaxServer.OnServerShutDown +=new IFaxServerNotify_OnServerShutDownEventHandler(objFaxServer_OnServerShutDown);
objFaxDrill.objFaxServer.OnOutgoingJobChanged +=new IFaxServerNotify_OnOutgoingJobChangedEventHandler(objFaxServer_OnOutgoingJobChanged);

ArrayList Values = new ArrayList();
string [] Files = Directory.GetFiles("C:\\FaxDrop\\","*");//give me everything in this directory...
for(intAryCtr=0;intAryCtr<Files.Length;intAryCtr++)
{
autoEvents = new AutoResetEvent[Files.Length];

FileName = Files[intAryCtr];
Values.Add(intAryCtr);


objFaxDrill.objFaxDocument.Body = FileName;

objFaxDrill.objFaxDocument.DocumentName = "C#.NET Fax widget";

objFaxDrill.objFaxDocument.Priority = FAXCOMEXLib.FAX_PRIORITY_TYPE_ENUM.fptHIGH;

objFaxDrill.objFaxDocument.Recipients.Add("999-999","Test Fax Recipient");

objFaxDrill.objFaxDocument.AttachFaxToReceipt = true;

objFaxDrill.objFaxDocument.CoverPageType = FAXCOMEXLib.FAX_COVERPAGE_TYPE_ENUM.fcptSERVER;
objFaxDrill.objFaxDocument.CoverPage = "generic";

objFaxDrill.objFaxDocument.Note = "Test C# Fax Server";

objFaxDrill.objFaxDocument.ScheduleTime = DateTime.Now;

objFaxDrill.objFaxDocument.Subject = "Today's fax";

objFaxDrill.objFaxDocument.Sender.Name = "Fax Server";
objFaxDrill.objFaxDocument.Sender.City = "Cucamonga";
objFaxDrill.objFaxDocument.Sender.State = "California";
objFaxDrill.objFaxDocument.Sender.Company = "Loony Toons";
objFaxDrill.objFaxDocument.Sender.Country = "USA";
objFaxDrill.objFaxDocument.Sender.Email = "(e-mail address removed)";
objFaxDrill.objFaxDocument.Sender.FaxNumber = "999-9999";
objFaxDrill.objFaxDocument.Sender.OfficeLocation = "cucamonga";
objFaxDrill.objFaxDocument.Sender.OfficePhone = "999-9999";
objFaxDrill.objFaxDocument.Sender.StreetAddress = "1313 Mocking Bird Lane.";
objFaxDrill.objFaxDocument.Sender.ZipCode = "91786";
objFaxDrill.objFaxDocument.Sender.Department = "BBunny - Fax Server";

objFaxDrill.objFaxDocument.Sender.SaveDefaultSender();

JobID = objFaxDrill.objFaxDocument.ConnectedSubmit(objFaxDrill.objFaxServer);

WaitHandle.WaitAll(autoEvents);
autoEvents[intAryCtr].Set();
}
}

catch (Exception ex)
{
throw (ex);
}
}//end SendFaxOrEmail()
}//end class FaxDrill.
}//end namespace FaxDrill.
 
Sample ManualResetEvent,
http://msdn.microsoft.com/library/d...emThreadingManualResetEventClassctorTopic.asp

Using ManualResetEvent in fax

public class SendFax
{
FaxServerClass faxServer;
ManualResetEvent addEvent = null;

void Register()
{
...........register for fax events..............
addEvent = new ManualResetEvent(false);
}

void SendFax()
{
.........Prepare Fax document and Send Fax .........
addEvent.WaitOne(MaxTimeToWait,false); //This will wait till
all faxes are submitted
..............
}

//Delegate, will be called when job is added
void OutJobAdded(FaxServer faxServer, string jobID)
{
numJobAdded++;
if (numJobAdded < numFaxes)
return;
addEvent.Set();
}

}

--
Loganatr [MSFT]
Microsoft Printing, Imaging and Fax Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.'



Patrick Kenney via AdminLife said:
this code works but I am having trouble with making it wait using
AutoResetEvent[] or ManualResetEvent any suggestions with a code snippet
of what to try?

Microsoft does not appear to have any C# samples of event handling with
their fax service api using C# and the c++ and vb6 ones do not translate
well to C#.

if I am missing something please advise...

thanks in advance.


namespace FaxDrill
{
public class FaxDrill
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);

public static AutoResetEvent[] autoEvents; //event array
public static int intAryCtr;

protected FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM FaxSvrEnum;
protected FAXCOMEXLib.FaxDocument objFaxDocument = new
FAXCOMEXLib.FaxDocumentClass();
protected FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM objFaxSvrEnum;
protected FAXCOMEXLib.FaxServer objFaxServer;
protected static object JobID;

public static void Main()
{
try
{
SendFaxOrEmail();
}

catch (Exception ex)
{
MessageBox(0,ex.Message + "source: " + ex.Source + "stack trace: " +
ex.StackTrace ,"error",0);
}

}

//event handlers
public static void objFaxServer_OnOutgoingJobAdded(FaxServer pFaxServer,
string bstrJobId)
{
MessageBox(0,"JobID: " + bstrJobId + "Added to queue","csFax",0);
}

public static void objFaxServer_OnServerShutDown(FaxServer pFaxServer)
{
MessageBox(0,"Fax Server Shutting Down","csFax",0);
}

public static void objFaxServer_OnOutgoingJobChanged(FaxServer pFaxServer,
string bstrJobId, FaxJobStatus pJobStatus)
{
if (pJobStatus.Status == FAXCOMEXLib.FAX_JOB_STATUS_ENUM.fjsCOMPLETED)
{
try
{
MessageBox(0,"Fax Job Completed","csFax",0);
autoEvents[intAryCtr].Set();
}

catch (Exception Ex)
{
MessageBox(0,Ex.Message,"thread error",0);
}
}

}//end onOutGoingJobChanged

public static void SendFaxOrEmail()
{
try
{
FaxDrill objFaxDrill = new FaxDrill();
string FileName;
string strSubString;

objFaxDrill.objFaxServer = new FAXCOMEXLib.FaxServer();
objFaxDrill.objFaxServer.Connect("");//empty string will default to local
host.

objFaxDrill.objFaxSvrEnum =
(FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM.fsetOUT_QUEUE |
FAXCOMEXLib.FAX_SERVER_EVENTS_TYPE_ENUM.fsetFXSSVC_ENDED);

//Register this method to listen to fax status events.
objFaxDrill.objFaxServer.ListenToServerEvents(objFaxDrill.objFaxSvrEnum);
/*
Delegates.
To wire the events, create a delegate instance and add it to the event you
want to raise.
E.G.: OnOutgoingJobAdded event.
*/
objFaxDrill.objFaxServer.OnOutgoingJobAdded +=new
IFaxServerNotify_OnOutgoingJobAddedEventHandler(objFaxServer_OnOutgoingJobAdded);
objFaxDrill.objFaxServer.OnServerShutDown +=new
IFaxServerNotify_OnServerShutDownEventHandler(objFaxServer_OnServerShutDown);
objFaxDrill.objFaxServer.OnOutgoingJobChanged +=new
IFaxServerNotify_OnOutgoingJobChangedEventHandler(objFaxServer_OnOutgoingJobChanged);

ArrayList Values = new ArrayList();
string [] Files = Directory.GetFiles("C:\\FaxDrop\\","*");//give me
everything in this directory...
for(intAryCtr=0;intAryCtr<Files.Length;intAryCtr++)
{
autoEvents = new AutoResetEvent[Files.Length];

FileName = Files[intAryCtr];
Values.Add(intAryCtr);


objFaxDrill.objFaxDocument.Body = FileName;

objFaxDrill.objFaxDocument.DocumentName = "C#.NET Fax widget";

objFaxDrill.objFaxDocument.Priority =
FAXCOMEXLib.FAX_PRIORITY_TYPE_ENUM.fptHIGH;

objFaxDrill.objFaxDocument.Recipients.Add("999-999","Test Fax Recipient");

objFaxDrill.objFaxDocument.AttachFaxToReceipt = true;

objFaxDrill.objFaxDocument.CoverPageType =
FAXCOMEXLib.FAX_COVERPAGE_TYPE_ENUM.fcptSERVER;
objFaxDrill.objFaxDocument.CoverPage = "generic";

objFaxDrill.objFaxDocument.Note = "Test C# Fax Server";

objFaxDrill.objFaxDocument.ScheduleTime = DateTime.Now;

objFaxDrill.objFaxDocument.Subject = "Today's fax";

objFaxDrill.objFaxDocument.Sender.Name = "Fax Server";
objFaxDrill.objFaxDocument.Sender.City = "Cucamonga";
objFaxDrill.objFaxDocument.Sender.State = "California";
objFaxDrill.objFaxDocument.Sender.Company = "Loony Toons";
objFaxDrill.objFaxDocument.Sender.Country = "USA";
objFaxDrill.objFaxDocument.Sender.Email = "(e-mail address removed)";
objFaxDrill.objFaxDocument.Sender.FaxNumber = "999-9999";
objFaxDrill.objFaxDocument.Sender.OfficeLocation = "cucamonga";
objFaxDrill.objFaxDocument.Sender.OfficePhone = "999-9999";
objFaxDrill.objFaxDocument.Sender.StreetAddress = "1313 Mocking Bird
Lane.";
objFaxDrill.objFaxDocument.Sender.ZipCode = "91786";
objFaxDrill.objFaxDocument.Sender.Department = "BBunny - Fax Server";

objFaxDrill.objFaxDocument.Sender.SaveDefaultSender();

JobID =
objFaxDrill.objFaxDocument.ConnectedSubmit(objFaxDrill.objFaxServer);

WaitHandle.WaitAll(autoEvents);
autoEvents[intAryCtr].Set();
}
}

catch (Exception ex)
{
throw (ex);
}
}//end SendFaxOrEmail()
}//end class FaxDrill.
}//end namespace FaxDrill.
 
Back
Top