如何从 COM 对象 (VB) 读取 Visual C# 2010 中的变体类型

发布于 2024-12-04 00:31:51 字数 1084 浏览 6 评论 0原文

我正在使用微软不久前发布的 FAXCOMEXLib 示例,并尝试将其移植到 C#。我在返回包含字符串数组的 Variant 类型的调用时遇到问题。 “jobID = objFaxDocument.ConnectedSubmit(objFaxServer);”

该过程返回一个消息框,其中显示“System.String[]”。我似乎记得在使用也使用变体的 Delphi 时,变体的一个属性只是一个显示其存储内容的字符串。也许这就是这里发生的事情。但无论如何,我怎样才能得到这个字符串数组并将其转换成 C# 可以理解的东西呢?

感谢

参考文献:
http://support.microsoft.com/kb/317030(显示如何从VB6) http://msdn.microsoft.com/en- us/library/ms692936(v=VS.85).aspx(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);

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 shows 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮坠入山谷 2024-12-11 00:31:51

一个简单的转换就可以解决问题:

object objIDs = objFaxDocument.ConnectedSubmit(objFaxServer);
string[] IDs = (string[])objID;

A simple cast will do the trick:

object objIDs = objFaxDocument.ConnectedSubmit(objFaxServer);
string[] IDs = (string[])objID;
爱冒险 2024-12-11 00:31:51

如果您知道它是一个字符串数组,只需将结果转换为 string[]:

string[] jobID = (string[]) objFaxDocument.ConnectedSubmit(objFaxServer);
MessageBox.Show("The Job ID is: " + jobID[0]);

传入的数据可能实际上是不同对象类型的数组(即异构数组)。在这种情况下,这将起作用(假设第一个元素确实是一个字符串):

object[] jobID = (object[]) objFaxDocument.ConnectedSubmit(objFaxServer);
MessageBox.Show("The Job ID is: " + (string)jobID[0]);

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]);
泪冰清 2024-12-11 00:31:51

如果您使用 C# 4.0,则应该使用动态“类型”进行研究

If you're working with C# 4.0, you should investigate using dynamic "type"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文