Why I have problems when calling OCX C++ code using C#

0

I have a problem when calling an OCX method using C#. OCX component is developed in C++.

I get this error:

"System.Runtime.InteropServices.COMException (0x80020005): Los tipos
 no coinciden. (Excepción de HRESULT: 0x80020005
 (DISP_E_TYPEMISMATCH))\r\n   en
 System.RuntimeType.ForwardCallToInvokeMember(String memberName,
 BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
 msgData)\r\n   en
 MFCActiveXControl1Lib._DMFCActiveXControl1.LeoCadena(IntPtr
 nombreFichero, IntPtr contenidoFichero, Int32 LongitudFichero)\r\n  
 en AxMFCActiveXControl1Lib.AxMFCActiveXControl1.LeoCadena(IntPtr
 nombreFichero, IntPtr contenidoFichero, Int32 longitudFichero) en
 c:\\Temp\\PrinterManager\\Test\\MFCActiveXControl1\\Debug\\AxMFCActiveXControl1Lib.cs:línea
 43\r\n   en TestPrinterManager.Form1.button2_Click(Object sender,
 EventArgs e) en
 C:\\Temp\\PrinterManager\\Test\\TestPrinterManager\\TestPrinterManager\\Form1.cs:línea
 77"

C++ signatures are:

*void flushFiles();
LONG LeoCadena(unsigned char * nombreFichero,
             unsigned char * contenidoFichero,
             int LongitudFichero);*

I have modified the generated visual studio wrapper so that the exported methods are:

IL file:

*void  flushFiles()
int32  LeoCadena(native int nombreFichero, 
             native int contenidoFichero,
             int32 LongitudFichero)*

cs file:

*public virtual void flushFiles()
public virtual int LeoCadena(System.IntPtr nombreFichero, System.IntPtr contenidoFichero, int 
                             longitudFichero)*

When I call the method using C#, I used this code:

*string file_xml = @"C:\proyectos\fichero.xml";
string texto_xml;
System.IO.StreamReader sr = new System.IO.StreamReader(file_xml);
texto_xml = sr.ReadToEnd();
sr.Close();
sr.Dispose();
byte[] arr = Encoding.ASCII.GetBytes(texto_xml);
int longitud = arr.Length; 
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * arr.Length);
Marshal.Copy(arr, 0, ptr, longitud);
axMFCActiveXControl1.LeoCadena(ptr, ptr, longitud);
Marshal.FreeHGlobal(ptr);*

Do you know why I get this error?

I use 32 bits platform and .Net Framework 4.5.2

c#
c++
com
interop
ocx
asked on Stack Overflow May 7, 2021 by Juan Luis López • edited May 7, 2021 by MickyD

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0