How to read variant type in Visual C# 2010 from COM object (VB)
I am working with the FAXCOMEXLib example that microsoft posted awhile back and am trying to port it to C#. I am having trouble with a call that returns a Variant type which holds a string array. "jobID = objFaxDocument.ConnectedSubmit(objFaxServer);"
The procedure returns a messagebox which shows "System.String[]". I seem to recall from working with Delphi which also uses variants, that one property of the variant is just a string which show开发者_C百科s what it is storing. Maybe this is what is going on here. But anyway, how can i get this string array out and convert it into something c# would understand?
Thanks
References:
http://support.microsoft.com/kb/317030 (shows how to return a variant from VB6) http://msdn.microsoft.com/en-us/library/ms692936(v=VS.85).aspx (example for FAXCOMEXLib) FAXCOMEXLib.FaxDocument objFaxDocument = new FAXCOMEXLib.FaxDocument();
FAXCOMEXLib.FaxServer objFaxServer = new FAXCOMEXLib.FaxServer();
object jobID;
try {
//Connect to the fax server
objFaxServer.Connect("");
// skipping some code, see MS example in URL above
jobID = objFaxDocument.ConnectedSubmit(objFaxServer);
MessageBox.Show("The Job ID is :" + jobID);
A simple cast will do the trick:
object objIDs = objFaxDocument.ConnectedSubmit(objFaxServer);
string[] IDs = (string[])objID;
If you know it's a string array, just cast the result to string[]:
string[] jobID = (string[]) objFaxDocument.ConnectedSubmit(objFaxServer);
MessageBox.Show("The Job ID is: " + jobID[0]);
It's possible that the incoming data is really an array of distinct object types (i.e., a heterogeneous array). In that case, this will work (assuming the first element is truly a string):
object[] jobID = (object[]) objFaxDocument.ConnectedSubmit(objFaxServer);
MessageBox.Show("The Job ID is: " + (string)jobID[0]);
If you're working with C# 4.0, you should investigate using dynamic "type"
精彩评论