问题标签 [interopservices]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1376 浏览

c# - 如何在 Windows 服务的锁定/解锁会话处理程序中使用 WtsApi32.dll?

我想使用 Windows 服务检测并关闭任何程序(例如:Notepad.exe)。下面的代码在控制台应用程序中是不错的选择。

但是上面的代码在 windows 服务 windows 7 中不起作用。看看这个链接:

http://social.msdn.microsoft.com/Forums/eu/netfxcompact/thread/04b16fac-043a-41c3-add9-482c912e95be

我在windows服务中编写了下面的代码,它不能在win 7上运行,它每次都在控制台应用程序的windows 7上工作。

我已经阅读了这篇文章(http://rhauert.wordpress.com/category/ucc/)但我不能使用

0 投票
1 回答
395 浏览

c# - 在 C# 中缩短 DllImport 的数量?

这是我的 C# 代码示例。有没有办法减少 DllImport 属性的数量?

0 投票
1 回答
205 浏览

.net - 为什么 System.Runtime.InteropServices.ClassInterfaceType 枚举中没有 AutoUnknown 值?

.NET 中的System.Runtime.InteropServices.ClassInterfaceType枚举有一个AutoDispatch值和一个AutoDual值,但没有AutoUnknown值。为什么不呢,有没有人已经想出了一种相当自动化的方法来完成它,这样我就不必重新发明轮子了?

对于更多背景知识,以下是当前枚举中的三个值以及它们的作用:

  • ClassInterfaceType.None不会为您创建类接口。这是 Microsoft 的推荐值。如果没有类接口,您必须使用您想要的任何成员创建自己的接口,并让您的类实现它。然后,您可以在接口上使用System.Runtime.InteropServices.ClassInterfaceAttribute属性使其成为分派接口(支持后期绑定客户端)、未知接口(支持早期绑定客户端)或双重接口(支持早期绑定客户端和后期绑定客户端)。
  • ClassInterfaceType.AutoDispatch创建一个派生自IDispatch没有显式成员的类接口。由于没有显式成员,它只能支持后期绑定的调用者。
  • ClassInterfaceType.AutoDual创建一个派生自该类接口的类接口,IDispatch该接口确实具有该类的所有公共非静态成员以及其基类和任何已实现接口的所有成员的显式成员。Microsoft 强烈反对使用这个值,“因为版本限制”,因为如果类的任何成员发生变化,类接口也会发生变化。(因此,在类更改后不重新绑定的调用者最终可能会调用错误的方法或传递不正确的参数。)

所以我想知道的是为什么没有一个调用的值ClassInterfaceType.AutoUnknown会创建一个派生自该类接口的类接口,该接口IUnknown明确声明您的类的成员(不是基类或它实现的其他接口)。

虽然我们在这里,但我也希望有ClassInterfaceType.AutoDualDirect类似 AutoDual 的东西,除了它不会公开基类中的所有成员和所有实现的接口,它只会公开类的直接成员,因为其他成员可以通过其他 COM 接口检索。

那么这里的故事是什么,我错过了什么?我知道微软表示,由于“版本控制挑战”,它建议不要使用 AutoDual,并且这些担忧在某种程度上也适用于 AutoUnknown。但尽管有这个建议,他们仍然AutoDual。为什么他们也没有 AutoUnknown?

关于那些“版本控制挑战”的一句话:这些问题中最危险的方面只适用于后期绑定的调用者。AutoUnknown 将生成一个 -IUnknown派生接口,而不是 -IDispatch派生接口,因此我们只需要关注早期绑定客户端可能遇到的版本控制问题。并且由于任何自动生成的类接口的 IID 都会在类的公共非静态成员发生变化时发生变化,因此早期绑定的客户端将无法对现有成员进行“错误调用”,它只会失败 QueryInterface( ) 用于类接口。仍然是失败,但可以管理,而不是崩溃或任何事情。这甚至没有打破不允许接口改变的 COM 规则;它没有改变,它正在成为一个新接口(新 IID)。的确,IUnknownAutoDual 机制的一半(减去派生成员)。事实上,它比 AutoDual 遇到的“版本控制挑战”更少,因为 AutoDual 也有所有后期绑定的挑战。

所以我不得不再次问:如果 AutoUnknown 不存在,为什么 AutoDual 存在!

为了给出一些实际示例,请考虑以下 C# 代码:

生成以下 idl:

很好,但我建议 AutoUnknown 将有助于生成此 idl:

如果现在没有办法自动执行此操作,我将编写一些使用反射来执行此操作的内容,因此我也会很感激有关这方面的任何建议。例如,我需要检查方法上的任何属性吗?关于参数?我深入研究了 ILSystem.Runtime.InteropServices.TypeLibConverter以了解它是如何做到的,但不幸的是,所有好东西都在我无法使用的私有内部调用 nConvertAssemblyToTypeLib() 函数中。

谢谢!

0 投票
2 回答
6426 浏览

c# - 如何将 const wchar_t* 从 C DLL 自动转换为 C# 字符串

这只是一种好奇心。或许这个世界上有一个人做过这样的事情:

我必须导出 C 函数并通过 DllImport 从 C# 代码加载它

最好的方法是声明 IntPtr,然后使用某些函数将其转换为字符串,并且被广泛推荐。换句话说,像这样

这种方法有效。但是有没有办法自动做到这一点?让 SysGetLibInfo 返回一个字符串?我发现了一些这样的建议:

但它不起作用,根据各种例子和很少的报告,它不应该起作用。

有没有办法编写我自己的属性,比如 MarshalAs,它将 IntPtr 转换为字符串?与此类似的东西:

提前感谢您提供任何信息或有用的链接、示例、书籍。同样,这只是一种好奇心。

PS 建议使用单独的函数包装 SysGetLibInfo,该函数使用 PtrToStringUni 将结果转换为字符串不是一种选择;)

0 投票
1 回答
1937 浏览

c# - GetWindowText() function is not executing correctly at all times

I'm writing a small application where I wish to get the URL from the Chrome browse.

In order to first check if the Chrome browser is open or not I use the following code:

I'm using GetWindowText() function to get the Windows title text, but I'm facing a problem there.

If the Chrome window has NO URL and is simply a New Tab then I have no issues, WindowTitleText.ToString() is equal to New Tab - Google Chrome.

However if I open a webpage, in which case the URL is filled with some URL then at the line GetWindowText() I get: vs32host.exe has stopped working message window asking for me to enter image description here

What's going on?

Help!

0 投票
0 回答
130 浏览

c# - 在 C# 中调用具有结构数组的非托管 C++ 代码

以下是我对该函数的 C++ 声明:

以下是我的 C++ 结构定义:

以下是我的 C# 客户端代码:

在这里,我无法获取我的结构数组值,即 inputdesarray、outputdesarray 和 interndesarray 中的值。

0 投票
2 回答
155 浏览

c# - Xml.Serializer illegal cast exception in C++ ActiveX control hosted in C# .Net 4.0 Application

I have a C# .Net 4.0 Application hosting a C++ ActiveX control utilizing a C++ DLL with CLR enabled. The DLL has the main function of loading the parameters for the OCX and uses XML.Serializer for this purpose.

This stack is working fine when all components are built in MS Visual Studio .Net 2003 and the C# Application running in .Net 1.1.

However, when the entire modules are migrated to VS2010 and the Application to .Net 4.0, I get the dreaded Xml.Serializer illegal cast exception because of the mismatched context.

The exception occurs at the 4th line:

Here's the exception statement:

at ParameterModule.ParaClass.execute_DeSerialize() Exception was thrown.

Please note that the 'LoadNeither' context has location path with the tilde (~) character. The 'Default' context has the full path.

The interop DLL for the ActiveX control is automatically generated by VS2010.

I wonder what causes the exception. Is it the mismatch in Path? I'm not sure, but I think the DLL was loaded only once.

Or is it the mismatch in context? If it is because of the context mismatch, how do we make sure the loading context for Interop modules like the C++ ActiveX controls? Or, can we specify Xml.Serializer to load the DLL containing the Serializing classes in the Default context?

I've looked everywhere and I could not find the solution. The more I comb the internet, the more this become a mystery to me. Thanks in advance.

0 投票
1 回答
58 浏览

idispatch - 实现 IDispatch 的“公共”与“内部”C# 类

我正在编写一个用作 IE WebBrowser 容器的类,它也将实现 IDispatch 接口,因此它的一些方法具有 DispID 属性:

这可以正常工作,并且仅当类为“公共”时才调用 Idispatch_AmbiantDlControl 方法。如果我将其声明为“内部”,则不再收到 IDispatch 调用。有人可以解释为什么吗?有什么办法吗?我不希望这个类可以公开访问。

提前致谢。

0 投票
0 回答
60 浏览

macos - 如何使用 mono 和 Runtime.Interopservices 从 C 库中导入定义?

我对 C-Sharp 完全不熟悉,所以这对我来说是全新的,但我有一个我正在尝试与单声道一起使用的框架。

我可以很好地从框架中导入函数,但是有很多例如类型定义(以 C 预处理器指令的形式,我认为这不会产生任何影响)和所有错误代码的定义。

现在一个想法是只在项目中包含头文件,但我认为有一种方法可以直接从框架中导入它们,你如何去做呢?

顺便说一句,我正在使用 Xamarin Studio 在 Mac OS 10.9.1 上工作,因此任何 Visual Studio 特定的解决方案都将无效。

0 投票
4 回答
2065 浏览

c# - 隐藏窗口中的最小化、最大化、关闭按钮并显示图标

我试图隐藏窗口顶部的最小化、最大化和关闭按钮,但仍然显示我的图标。

我尝试了几种不同的方法,但无法保留图标。这是我正在使用的代码:

任何帮助将不胜感激!谢谢!