CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2000
    Location
    Germany, Franken
    Posts
    257

    how to call a c++ function with wchar_t[] parameter

    Hi,
    I've got a unmanaged C++ DLL which exports 1 function:
    Code:
    extern "C" bool GetDatabaseInfo( UINT uiBuffCnt,
                                     wchar_t wcDataSource[], wchar_t wcCatalog[],
                                     wchar_t wcUsername[], wchar_t wcPassword[] )
    Now I've got to call this function from c#. I tried it this way:
    Code:
    [ DllImport( "..\\mydll.dll" ) ]
        internal static extern bool GetDatabaseInfo( uint uiBuffCnt,
                                                     ref string[] sDataSource,
                                                     ref string[] sCatalog,
                                                     ref string[] sUsername,
                                                     ref string[] sPassword
                                                   );
    
    string[] sDataSource = new string[1024], 
                     sCatalog    = new string[1024],
                     sUsername   = new string[1024],
                     sPassword   = new string[1024];
     GetDatabaseInfo( 1024, ref sDataSource, ref sCatalog, ref sUsername, ref sPassword );
    But the call always fails. Can anyone give me a hint what i did wrong?

    Thanks in Advance

    Akademos

  2. #2
    Join Date
    Aug 2002
    Location
    Redmond, WA, USA
    Posts
    88
    Do you realize that the sample you provided creates a total of 4096 strings? When you do a "new string[NN]" you are allocating memory for an array of NN strings.

    Also, the C type of wchar_t is a wide character. Therefore, your C function takes an unsigned integer and 4 wide character arrays. The wide character arrays are usually single UNICODE strings.

    In the C# application, prototype the function to take strings only (not string arrays) and it should be fine.

    Hope that helps.

    - Robert

  3. #3
    Join Date
    Mar 2000
    Location
    Germany, Franken
    Posts
    257
    You're right, what a stupidy of mine.
    This is the correct code:
    Code:
    [ DllImport( "..\\mydll.dll", CharSet=CharSet.Unicode ) ]
        internal static extern bool GetDatabaseInfo( uint uiBuffCnt,
                                                     string sDataSource,
                                                     string sCatalog,
                                                     string sUsername,
                                                     string sPassword
                                                   );
                                                   
    //----------------------------------------------------------------------
    
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    		  
          string sDataSource = new string( ' ', 1024 );
          string sCatalog = new string( ' ', 1024 );
          string sUsername = new string( ' ', 1024 );
          string sPassword = new string( ' ', 1024 );
          try
          { 
            //sDataSource = sCatalog = sUsername = sPassword = "";         
            GetDatabaseInfo( 1024, m_sDataSource,m_sCatalog,m_sUsername,m_sPassword );
         }
    But now I've go the problem that the returning strings a filled with '\0' behind the text when the given length is greater than the actual length.
    Now i want to trime the returning string to a fitting size. How can I do that?

    Thanks in Advance

    Akademos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured