struct in a C# dll used in a vb application



  • Hi,

    I have a self defined struct in my C# dll, it will be used by a vb application.
    I got error: type mismatch, by debugging the VB code.

    Here is my code:

    // C# DLL code
    namespace CallerSpace
    {
    
        public interface Icall
        {
            void getSentence(myDATA d);
        }
    
       [StructLayout(LayoutKind.Sequential)]
        public struct myDATA
        {
            public int time;
            public int Status;
        }
    
        public class CALL: Icall
        {
    
            public void getSentence(myDATA d)
            {
                 ......        
            }
    
        }
    
    }
    
    // VB Application code
    Sub Main
    	Dim call As CallerSpace.Icall
    	Set call = New CallerSpace.CALL
    
    	Dim mdata As myDATA
    	mdata.Status = 1
    	mdata.time = 122
    
    	call.getSentence(mdata)  // here: error > type mismatch
    
    End Sub
    

    Any one know, where comes from the error?

    Thank you!!

    bag



  • Are NMEA_RMC_DATA and DATA different?

    // C# DLL code
    namespace CallerSpace
    {
    
        public class CALL: Icall
        {
    
            public void getSentence(NMEA_RMC_DATA d)
            {
                 ......        
            }
    
        }
    
    }
    


  • Sorry witte,

    i made the mistake. I typed too quickly. Now I corrected the code.

    But still the problem type mismach.

    Any idea?

    Regards,

    bag

    witte schrieb:

    Are NMEA_RMC_DATA and DATA different?

    [/cpp]



  • my problem maybe : I do not know how to pass the struct type across the DLL boundary.

    witte schrieb:

    Are NMEA_RMC_DATA and DATA different?

    // C# DLL code
    


  • Hi guys,

    I have solved the problem.

    Just pass the struct as a reference. Then no error popup.

    change
    
    void getSentence(myDATA d) 
    
    to
    void getSentence(ref myDATA d);
    

    🙂 🙂 🙂

    Regards,

    bag



  • The code won't work in VB because `Call` is a reserved word here. Either you change the variable name or you escape it by putting it in braces:

    Dim [call] As CallerSpace.Icall
    

    However, I'm not sure if the current version of VBA does support escaping variable names. VB6 didn't (only for Enum constants).


Anmelden zum Antworten