COM:使用IntPtr类型从c#到c ++问题的接口 [英] COM : Interface from c# to c++ problem with IntPtr type
问题描述
大家好,
我有一个加载C#applets的C ++应用程序。一个方法使用IntPtr
参数,因为我想要32/64位兼容。问题是
生成的.tlh文件。这个参数很长,而不是INT_PTR
,我不知道为什么?
这里有一些代码可以帮到你。
在C#项目中。
公共接口IDlgCriteresRecherche
{
IntPtr HandleEditeurParent {get;设置;}
}
公共类AScDlgCriteresRecherche:System.Windows.Forms.Form,
IDlgCriteresRecherche
{
#region IDlgCriteresRecherche会员
私人IntPtr nHandleEditeurParent =(IntPtr)0;
public IntPtr HandleEditeurParent
{
get {return nHandleEditeurParent; }
set {nHandleEditeurParent = value; }
}
#endregion
}
在生成的.tlh中
struct __declspec(uuid(" a070cc19-98cf-4433-8351-e1fc2228260f"))
/ *双接口* / IDlgCriteresRecherche;
struct / * coclass * / AScDlgCriteresRecherche;
_COM_SMARTPTR_TYPEDEF(IDlgCriteresRecherche,
__uuidof(IDlgCriteresRecherche));
>
struct __declspec(uuid(" a070cc19-98cf-4433-8351-e1fc2228260f"))
IDlgCriteresRecherche:IDispatch
{
虚拟HRESULT __stdcall get_HandleEditeurParent(
/ * [out,retval] * / long * pRetVal)= 0;
虚拟HRESULT __stdcall put_HandleEditeurParent(
/ * [in] * / long pRetVal)= 0;
}
in C ++
#import" ASCommun.tlb" raw_interfaces_only
pIDlgCriteresRecherche-> put_HandleEditeurParent((INT_PTR)poActiveView-> m_hWnd);
问候,
>
Eric
Hi all,
I have a C++ application that load C# applets. A method use an IntPtr
parameter because I want to be 32/64-bit compatible. The problem is in
the .tlh file generated. This parameter is a long and not an INT_PTR
and I don''t know why ?
Here is some code that may help you.
In C# project.
public interface IDlgCriteresRecherche
{
IntPtr HandleEditeurParent { get; set;}
}
public class AScDlgCriteresRecherche : System.Windows.Forms.Form,
IDlgCriteresRecherche
{
#region IDlgCriteresRecherche Members
private IntPtr nHandleEditeurParent = (IntPtr)0;
public IntPtr HandleEditeurParent
{
get{ return nHandleEditeurParent; }
set{ nHandleEditeurParent = value; }
}
#endregion
}
In the .tlh generated
struct __declspec(uuid("a070cc19-98cf-4433-8351-e1fc2228260f"))
/* dual interface */ IDlgCriteresRecherche;
struct /* coclass */ AScDlgCriteresRecherche;
_COM_SMARTPTR_TYPEDEF(IDlgCriteresRecherche,
__uuidof(IDlgCriteresRecherche));
struct __declspec(uuid("a070cc19-98cf-4433-8351-e1fc2228260f"))
IDlgCriteresRecherche : IDispatch
{
virtual HRESULT __stdcall get_HandleEditeurParent (
/*[out,retval]*/ long * pRetVal ) = 0;
virtual HRESULT __stdcall put_HandleEditeurParent (
/*[in]*/ long pRetVal ) = 0;
}
in C++
#import "ASCommun.tlb" raw_interfaces_only
pIDlgCriteresRecherche->put_HandleEditeurParent((INT_PTR)poActiveView->m_hWnd);
Regards,
Eric
推荐答案
Eric。
你''重做COM互操作,COM中的int是16位。 .NET中的int
是32位,这在COM中很长。这与您的
应用程序是针对32位还是64位处理器无关。
Stephan
Eric写道:
Hi, Eric.
You''re doing COM interop, and an int in COM is 16 bits. An int in .NET
is 32 bits, which is a long in COM. This is independent of whether your
app is targeted at 32 or 64 bit processors.
Stephan
Eric wrote:
大家好,
我有一个加载C#applets的C ++应用程序。一个方法使用IntPtr
参数,因为我想要32/64位兼容。问题是
生成的.tlh文件。这个参数很长,而不是INT_PTR
,我不知道为什么?
这里有一些代码可以帮到你。
在C#项目中。
公共接口IDlgCriteresRecherche
{
IntPtr HandleEditeurParent {get;设置;}
}
公共类AScDlgCriteresRecherche:System.Windows.Forms.Form,
IDlgCriteresRecherche
{
#region IDlgCriteresRecherche会员
私人IntPtr nHandleEditeurParent =(IntPtr)0;
public IntPtr HandleEditeurParent
{
get {return nHandleEditeurParent; }
set {nHandleEditeurParent = value; }
}
#endregion
}
在生成的.tlh中
struct __declspec(uuid(" a070cc19-98cf-4433-8351-e1fc2228260f"))
/ *双接口* / IDlgCriteresRecherche;
struct / * coclass * / AScDlgCriteresRecherche;
_COM_SMARTPTR_TYPEDEF(IDlgCriteresRecherche,
__uuidof(IDlgCriteresRecherche));
>
struct __declspec(uuid(" a070cc19-98cf-4433-8351-e1fc2228260f"))
IDlgCriteresRecherche:IDispatch
{
虚拟HRESULT __stdcall get_HandleEditeurParent(
/ * [out,retval] * / long * pRetVal)= 0;
虚拟HRESULT __stdcall put_HandleEditeurParent(
/ * [in] * / long pRetVal)= 0;
}
in C ++
#import" ASCommun.tlb" raw_interfaces_only
pIDlgCriteresRecherche-> put_HandleEditeurParent((INT_PTR)poActiveView-> m_hWnd);
问候,
>
Eric
Hi all,
I have a C++ application that load C# applets. A method use an IntPtr
parameter because I want to be 32/64-bit compatible. The problem is in
the .tlh file generated. This parameter is a long and not an INT_PTR
and I don''t know why ?
Here is some code that may help you.
In C# project.
public interface IDlgCriteresRecherche
{
IntPtr HandleEditeurParent { get; set;}
}
public class AScDlgCriteresRecherche : System.Windows.Forms.Form,
IDlgCriteresRecherche
{
#region IDlgCriteresRecherche Members
private IntPtr nHandleEditeurParent = (IntPtr)0;
public IntPtr HandleEditeurParent
{
get{ return nHandleEditeurParent; }
set{ nHandleEditeurParent = value; }
}
#endregion
}
In the .tlh generated
struct __declspec(uuid("a070cc19-98cf-4433-8351-e1fc2228260f"))
/* dual interface */ IDlgCriteresRecherche;
struct /* coclass */ AScDlgCriteresRecherche;
_COM_SMARTPTR_TYPEDEF(IDlgCriteresRecherche,
__uuidof(IDlgCriteresRecherche));
struct __declspec(uuid("a070cc19-98cf-4433-8351-e1fc2228260f"))
IDlgCriteresRecherche : IDispatch
{
virtual HRESULT __stdcall get_HandleEditeurParent (
/*[out,retval]*/ long * pRetVal ) = 0;
virtual HRESULT __stdcall put_HandleEditeurParent (
/*[in]*/ long pRetVal ) = 0;
}
in C++
#import "ASCommun.tlb" raw_interfaces_only
pIDlgCriteresRecherche->put_HandleEditeurParent((INT_PTR)poActiveView->m_hWnd);
Regards,
Eric
ssamuel< ss ***** @ gmail.comwrote:
ssamuel <ss*****@gmail.comwrote:
Eric。
你正在做COM互操作,COM中的int是16位。 .NET中的int
是32位,这在COM中很长。这与您的
应用程序是针对32位还是64位处理器无关。
Hi, Eric.
You''re doing COM interop, and an int in COM is 16 bits. An int in .NET
is 32 bits, which is a long in COM. This is independent of whether your
app is targeted at 32 or 64 bit processors.
COM中的int是32位。 COM中的短路是16位。 C ++中的长整数是平台的字长(x86为32位,x86_64为64位)。我不确定这对COM中的长期有多大影响,但我怀疑在C / C ++中跟随
相同的约定。
< br $>
-
Thomas T. Veldhouse
主要指纹:D281 77A5 63EE 82C5 5E68 00E4 7868 0ADC 4EFB 39F0
An int in COM is 32 bits. A short in COM is 16 bits. A long in C++ is the
word length of your platform (32 bits on x86 and 64 bits on x86_64). I am not
sure off hand how this affects long in COM, but I suspect long follows the
same convention as it does in C/C++.
--
Thomas T. Veldhouse
Key Fingerprint: D281 77A5 63EE 82C5 5E68 00E4 7868 0ADC 4EFB 39F0
>
你正在进行COM互操作,COM中的int是16位。 .NET中的int
是32位,这在COM中很长。这与您的
应用程序是针对32位还是64位处理器无关。
>
You''re doing COM interop, and an int in COM is 16 bits. An int in .NET
is 32 bits, which is a long in COM. This is independent of whether your
app is targeted at 32 or 64 bit processors.
好的,所以要兼容32 / 64-我必须在.NET中使用Int64而不是在64位处理器的情况下使用指针troncation吗?
我认为IntPtr更方便
Eric
Ok, so to be compatible 32/64-bit I have to use Int64 in .NET for not
having pointer troncation in the case of 64-bit processors?
I thought IntPtr was more convenient
Eric
这篇关于COM:使用IntPtr类型从c#到c ++问题的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!