473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OCX(COM) Interop...Passi ng pointer of structure to function

Hi,

I have the following question regarding communicating with a OCX control
using C#. Idon't have access to the ocx code, so my c# code is actually mimic
the behavior of C++ counterparts. Basically, I have problem passing a
structure to a function.

Here is the C++ part code:

//*************** *************** **********
typedef struct
{
char TypeID;
char UserName[20];
char Password[20];
char UrlAddress[255];
int iStreamChannel;
bool bDirectDrawMode ;
}CONNECT_DATA;

CONNECT_DATA m_ConnectData;

// after initializing m_ConnectData

int ret= InitConnectData ((PBYTE)&m_Conn ectData);

//*************** *************** **********

In C# when I drop this active control to my app, the interface for the
function
InitConnectData has be given as follows

InitConnectData (ref byte)

So the following is what I did in the C# part.

//*************** *************** **********

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi,Pack =8)]
public unsafe struct CONNECT_DATA
{
public byte TypeID;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string UserName;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string Password;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 255)]
public string UrlAddress;

public int iStreamChannel;
public bool bDirectDrawMode ;

}

CONNECT_DATA connect_data = new CONNECT_DATA();

// After initializing all the fields

IntPtr pnt = Marshal.AllocCo TaskMem(Marshal .SizeOf(connect _data));
try
{
Marshal.Structu reToPtr(connect _data, pnt, false);
byte[] buffer = new byte[Marshal.SizeOf( connect_data)];
Marshal.Copy(pn t, buffer, 0, Marshal.SizeOf( connect_data));
int ret = axNetworkCamera Link2.InitConne ctData(ref buffer[0]);
}
//*************** *************** **********

But it seems that the function never gets the structure right, for it always
gives the wrong return value.

I tried to "Tlbimp" the original DLL, change the IL code so that the
"InitConnectDat a(uint8&)" becames "InitConnectDat a(native int)", and in C#
the interface becames "InitConnectDat a(System.IntPtr )", but error occurs,
stating that

"System.Runtime .InteropService s.COMException
(0x80020005): Type mismatch. at
System.RuntimeT ype.ForwardCall ToInvokeMember( String memberName, BindingFlags
flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)"

I guess it is because uint8 is 8 bit and native int is 32 bit...I really
don't know
what to do now, and I think I must do something wrong...Please help, thanks!

Sep 25 '08 #1
3 2750
Dont know about the actual call but I can see a few problems with your
structure definition :

1. The UserName,passwo rd and address are defined as ByValTStr (means Ansi or
Uni depending on platform) when in fact they are defined as ANSI in the
original structure.

2. The bDirectDrawMode is defined as bool (which marshals to 4 bytes) but
the origianl structure defines it as smallcase bool which in C++ I think
occupies 1 byte.

---------
- G Himangi, LogicNP Software http://www.ssware.com
Shell MegaPack: GUI Controls For Drop-In Windows Explorer like File/Folder
Browsing Functionality (.Net & ActiveX Editions).
EZNamespaceExte nsions: Develop namespace extensions rapidly in .Net and
MFC/ATL/C++
EZShellExtensio ns: Develop all shell extensions,expl orer bars and BHOs
rapidly in .Net & MFC/ATL/C++
---------

"Mingyi" <Mi****@discuss ions.microsoft. comwrote in message
news:4C******** *************** ***********@mic rosoft.com...
Hi,

I have the following question regarding communicating with a OCX control
using C#. Idon't have access to the ocx code, so my c# code is actually
mimic
the behavior of C++ counterparts. Basically, I have problem passing a
structure to a function.

Here is the C++ part code:

//*************** *************** **********
typedef struct
{
char TypeID;
char UserName[20];
char Password[20];
char UrlAddress[255];
int iStreamChannel;
bool bDirectDrawMode ;
}CONNECT_DATA;

CONNECT_DATA m_ConnectData;

// after initializing m_ConnectData

int ret= InitConnectData ((PBYTE)&m_Conn ectData);

//*************** *************** **********

In C# when I drop this active control to my app, the interface for the
function
InitConnectData has be given as follows

InitConnectData (ref byte)

So the following is what I did in the C# part.

//*************** *************** **********

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi,Pack =8)]
public unsafe struct CONNECT_DATA
{
public byte TypeID;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string UserName;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string Password;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 255)]
public string UrlAddress;

public int iStreamChannel;
public bool bDirectDrawMode ;

}

CONNECT_DATA connect_data = new CONNECT_DATA();

// After initializing all the fields

IntPtr pnt = Marshal.AllocCo TaskMem(Marshal .SizeOf(connect _data));
try
{
Marshal.Structu reToPtr(connect _data, pnt, false);
byte[] buffer = new byte[Marshal.SizeOf( connect_data)];
Marshal.Copy(pn t, buffer, 0, Marshal.SizeOf( connect_data));
int ret = axNetworkCamera Link2.InitConne ctData(ref
buffer[0]);
}
//*************** *************** **********

But it seems that the function never gets the structure right, for it
always
gives the wrong return value.

I tried to "Tlbimp" the original DLL, change the IL code so that the
"InitConnectDat a(uint8&)" becames "InitConnectDat a(native int)", and in C#
the interface becames "InitConnectDat a(System.IntPtr )", but error occurs,
stating that

"System.Runtime .InteropService s.COMException
(0x80020005): Type mismatch. at
System.RuntimeT ype.ForwardCall ToInvokeMember( String memberName,
BindingFlags
flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)"

I guess it is because uint8 is 8 bit and native int is 32 bit...I really
don't know
what to do now, and I think I must do something wrong...Please help,
thanks!

Sep 25 '08 #2

Hi,

I think the first problem is solved by the declaration
"CharSet=CharSe t.Ansi" (I think..)

For the second one, I used to marshall the bool to 1 byte counterpart, but
the problem persists..

Thanks for the reply anyway.
"G Himangi" wrote:
Dont know about the actual call but I can see a few problems with your
structure definition :

1. The UserName,passwo rd and address are defined as ByValTStr (means Ansi or
Uni depending on platform) when in fact they are defined as ANSI in the
original structure.

2. The bDirectDrawMode is defined as bool (which marshals to 4 bytes) but
the origianl structure defines it as smallcase bool which in C++ I think
occupies 1 byte.

---------
- G Himangi, LogicNP Software http://www.ssware.com
Shell MegaPack: GUI Controls For Drop-In Windows Explorer like File/Folder
Browsing Functionality (.Net & ActiveX Editions).
EZNamespaceExte nsions: Develop namespace extensions rapidly in .Net and
MFC/ATL/C++
EZShellExtensio ns: Develop all shell extensions,expl orer bars and BHOs
rapidly in .Net & MFC/ATL/C++
---------

"Mingyi" <Mi****@discuss ions.microsoft. comwrote in message
news:4C******** *************** ***********@mic rosoft.com...
Hi,

I have the following question regarding communicating with a OCX control
using C#. Idon't have access to the ocx code, so my c# code is actually
mimic
the behavior of C++ counterparts. Basically, I have problem passing a
structure to a function.

Here is the C++ part code:

//*************** *************** **********
typedef struct
{
char TypeID;
char UserName[20];
char Password[20];
char UrlAddress[255];
int iStreamChannel;
bool bDirectDrawMode ;
}CONNECT_DATA;

CONNECT_DATA m_ConnectData;

// after initializing m_ConnectData

int ret= InitConnectData ((PBYTE)&m_Conn ectData);

//*************** *************** **********

In C# when I drop this active control to my app, the interface for the
function
InitConnectData has be given as follows

InitConnectData (ref byte)

So the following is what I did in the C# part.

//*************** *************** **********

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi,Pack =8)]
public unsafe struct CONNECT_DATA
{
public byte TypeID;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string UserName;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string Password;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 255)]
public string UrlAddress;

public int iStreamChannel;
public bool bDirectDrawMode ;

}

CONNECT_DATA connect_data = new CONNECT_DATA();

// After initializing all the fields

IntPtr pnt = Marshal.AllocCo TaskMem(Marshal .SizeOf(connect _data));
try
{
Marshal.Structu reToPtr(connect _data, pnt, false);
byte[] buffer = new byte[Marshal.SizeOf( connect_data)];
Marshal.Copy(pn t, buffer, 0, Marshal.SizeOf( connect_data));
int ret = axNetworkCamera Link2.InitConne ctData(ref
buffer[0]);
}
//*************** *************** **********

But it seems that the function never gets the structure right, for it
always
gives the wrong return value.

I tried to "Tlbimp" the original DLL, change the IL code so that the
"InitConnectDat a(uint8&)" becames "InitConnectDat a(native int)", and in C#
the interface becames "InitConnectDat a(System.IntPtr )", but error occurs,
stating that

"System.Runtime .InteropService s.COMException
(0x80020005): Type mismatch. at
System.RuntimeT ype.ForwardCall ToInvokeMember( String memberName,
BindingFlags
flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)"

I guess it is because uint8 is 8 bit and native int is 32 bit...I really
don't know
what to do now, and I think I must do something wrong...Please help,
thanks!


Sep 25 '08 #3
>
I think the first problem is solved by the declaration
"CharSet=CharSe t.Ansi" (I think..)
Yes, you are right.

Another problem I noticed was the Pack=8 attribute. Is this type of packing
specified on the original structure? If not, leave it out.

---------
- G Himangi, LogicNP Software http://www.ssware.com
Shell MegaPack: GUI Controls For Drop-In Windows Explorer like File/Folder
Browsing Functionality (.Net & ActiveX Editions).
EZNamespaceExte nsions: Develop namespace extensions rapidly in .Net and
MFC/ATL/C++
EZShellExtensio ns: Develop all shell extensions,expl orer bars and BHOs
rapidly in .Net & MFC/ATL/C++
---------

"Mingyi" <Mi****@discuss ions.microsoft. comwrote in message
news:B7******** *************** ***********@mic rosoft.com...
>
Hi,

I think the first problem is solved by the declaration
"CharSet=CharSe t.Ansi" (I think..)

For the second one, I used to marshall the bool to 1 byte counterpart, but
the problem persists..

Thanks for the reply anyway.
"G Himangi" wrote:
>Dont know about the actual call but I can see a few problems with your
structure definition :

1. The UserName,passwo rd and address are defined as ByValTStr (means Ansi
or
Uni depending on platform) when in fact they are defined as ANSI in the
original structure.

2. The bDirectDrawMode is defined as bool (which marshals to 4 bytes) but
the origianl structure defines it as smallcase bool which in C++ I think
occupies 1 byte.

---------
- G Himangi, LogicNP Software http://www.ssware.com
Shell MegaPack: GUI Controls For Drop-In Windows Explorer like
File/Folder
Browsing Functionality (.Net & ActiveX Editions).
EZNamespaceExt ensions: Develop namespace extensions rapidly in .Net and
MFC/ATL/C++
EZShellExtensi ons: Develop all shell extensions,expl orer bars and BHOs
rapidly in .Net & MFC/ATL/C++
---------

"Mingyi" <Mi****@discuss ions.microsoft. comwrote in message
news:4C******* *************** ************@mi crosoft.com...
Hi,

I have the following question regarding communicating with a OCX
control
using C#. Idon't have access to the ocx code, so my c# code is actually
mimic
the behavior of C++ counterparts. Basically, I have problem passing a
structure to a function.

Here is the C++ part code:

//*************** *************** **********
typedef struct
{
char TypeID;
char UserName[20];
char Password[20];
char UrlAddress[255];
int iStreamChannel;
bool bDirectDrawMode ;
}CONNECT_DATA;

CONNECT_DATA m_ConnectData;

// after initializing m_ConnectData

int ret= InitConnectData ((PBYTE)&m_Conn ectData);

//*************** *************** **********

In C# when I drop this active control to my app, the interface for the
function
InitConnectData has be given as follows

InitConnectData (ref byte)

So the following is what I did in the C# part.

//*************** *************** **********

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi,Pack =8)]
public unsafe struct CONNECT_DATA
{
public byte TypeID;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string UserName;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 20)]
public string Password;

[MarshalAs(Unman agedType.ByValT Str, SizeConst = 255)]
public string UrlAddress;

public int iStreamChannel;
public bool bDirectDrawMode ;

}

CONNECT_DATA connect_data = new CONNECT_DATA();

// After initializing all the fields

IntPtr pnt =
Marshal.AllocCo TaskMem(Marshal .SizeOf(connect _data));
try
{
Marshal.Structu reToPtr(connect _data, pnt, false);
byte[] buffer = new byte[Marshal.SizeOf( connect_data)];
Marshal.Copy(pn t, buffer, 0,
Marshal.SizeOf( connect_data));
int ret = axNetworkCamera Link2.InitConne ctData(ref
buffer[0]);
}
//*************** *************** **********

But it seems that the function never gets the structure right, for it
always
gives the wrong return value.

I tried to "Tlbimp" the original DLL, change the IL code so that the
"InitConnectDat a(uint8&)" becames "InitConnectDat a(native int)", and in
C#
the interface becames "InitConnectDat a(System.IntPtr )", but error
occurs,
stating that

"System.Runtime .InteropService s.COMException
(0x80020005): Type mismatch. at
System.RuntimeT ype.ForwardCall ToInvokeMember( String memberName,
BindingFlags
flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)"

I guess it is because uint8 is 8 bit and native int is 32 bit...I
really
don't know
what to do now, and I think I must do something wrong...Please help,
thanks!



Sep 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
5414
by: Steve | last post by:
Can anyone point me to a primer for creating OCX controls in VB .net? In particular, I want to access a web service in a VB6 application (not an easy thing to do). So, if I can write an ActiveX control that accomplishes this in .Net, then I SHOULD be able to add the OCX component to my VB6 app and use it. Unless someone knows why this wouldn't work.... Any and all help is appreciated.
0
1071
by: Sameer | last post by:
I decided to strong name the axhost generated when I inserted the an activex control in my windows form. To this end... 1. Created Interop Interop.MyAnalogLibrary.dll tlbimp \winnt\system32\MyAnalogLibrary.ocx /out:Interop.MyAnalogLibrary.dll /keyfile:..\..\1.snk 2. Created AxInterop AxInterop.MyAnalogLibrary.dll aximp \winnt\system32\MyAnalogLibrary.ocx /out:AxInterop.MyAnalogLibrary.dll /rcw:Interop.MyAnalogLibrary.dll
4
2090
by: javaid iqbal | last post by:
i want to use video.ocx for reading camera in my program. i was using it very succesfully in VB 6.0. Can some body guide me how to use it in CSharp. Thanks in Advance.
15
6745
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use javascript or vbscript it works. I will appreciate any help. I do the following (C#):
0
2482
by: MarkD | last post by:
I have an ASP.NET (VB.NET) application that calls all VB6 COM DLL via Interop. The DLL uses functionality contained in a Custom OCX Control (Also VB6) that in turn contains a standard TreeView control. The Custom OCX is not used for visual purposes, but has some functionality that we require (hence hosting an OCX in a DLL). Everything worked fine until we installed the latest service pack for each respective windows operating system:...
1
2009
by: marfi95 | last post by:
I'm working on a demo app for our customer with various hardware devices. The app needs to be in vb.net, but the vendor supplied interface is an .ocx. Is there some doco that someone can point me to that explains how to use the interop services to call a VB6 .ocx file ? For example, do I just need an interop wrapper for the ocx or every method within the ocx ? I guess I just need a good example or pointing to a good site for doco. ...
2
9202
by: Alex Su | last post by:
Hello all, I'm new to VB.net and have just built a small application that makes use of some VB6 ocx such as mscomm32.ocx for data communication with a machine. The application runs OK but when I tried to build the installer using the Setup and Deployment Projects, I always get the following warning messages saying that the dependencies cannot be found as follows: WARNING: Unable to find dependency 'INTEROP.RICHTEXTLIB'
1
2662
by: omar.torres | last post by:
Hi all, Currently I have a VB6 application which uses a custom OCX. To use the ocx in my VB6 application, I used the following code: Public MyOCXInstance As OCXobject 'Create a new instance of the OCX ..... Function read() Dim MyString as string MyOCXInstance.read(MyString)
3
1894
by: =?Utf-8?B?amFtZXNAbm9zcGFtLmNvbQ==?= | last post by:
I have an existing MFC c++ ocx control that I need to work with a c# class. If I use a locally defined data structure, everything works. For example, in my C++ app I declare. MyCSharpClass DataStructure; To use the data structure in my c# class I use DataAccess::Select(%DataStructure);
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.