Link to home
Start Free TrialLog in
Avatar of jasonvv
jasonvv

asked on

Retrieving values from DLLIMPORT function call.

I am having a difficult time getting a value out of a C++ DLL Call. I am able to call functions that do not return a string just fine, so I am 90% there. Please help!

The header file for the function I am call is as follows:

extern "C" int FAR PASCAL EXPORT GetFieldValue(const char* chFieldName,
                                                 char* chReturnValue,
                                                 int nMaxSize);

My code looks like this:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;

namespace PFAPI
{
      [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
      public struct GetStruct
      {      
            public string filenum;
            public string buffer;
            public int size;
      }

public class Form1 : System.Windows.Forms.Form
{
      [DllImport("PFAPI.dll", CharSet=CharSet.Ansi )]
      public static extern int GetFieldValue( ref GetStruct mss );

private string GetVal(string filenum)
{
      int retVal;

      StringBuilder buffer = new StringBuilder( 255 );

      GetStruct mss = new GetStruct();

      mss.filenum = filenum.ToString();
      mss.buffer = buffer.ToString();
      mss.size = buffer.Capacity;

      retVal = GetFieldValue( ref mss );

      return mss.buffer.ToString();
}
}
}

The buffer is always blank and the value or 'retVal' is always zero. As I mentioned I have been successful getting every other call that does not return a string so I think I am just incorrectly handling/defining the passed string.

Thank you!
Avatar of dungla
dungla
Flag of Viet Nam image

The function that you defined in C++ receive 3 params, so you should be passed 3 param to your imported function in C#.

For example:

 [DllImport("PFAPI.dll", CharSet=CharSet.Ansi )]
     public static extern int GetFieldValue( ref GetStruct mss );
Change to:
 [DllImport("PFAPI.dll", CharSet=CharSet.Ansi )]
     public static extern int GetFieldValue( ref string strFileName, ref string strReturn, int nMaxSize );

Or define the struct that used in C# function in your C++ header to receive the data

public struct GetStruct
     {    
          public string filenum;
          public string buffer;
          public int size;
     }

and then the C++ function should be

extern "C" int FAR PASCAL EXPORT GetFieldValue(const GetStruct* mss);

Just try it
You should add the [ StructLayout( LayoutKind.Sequential)] attribute to the struct.
Avatar of jasonvv
jasonvv

ASKER

Is my StringBuilder code correct to get the data? Can I do this:

private string GetVal(string filenum)
{
     int retVal;

     StringBuilder buffer = new StringBuilder( 255 );

     retVal = GetFieldValue( filenum.ToString(), buffer.ToString(), buffer.length );

     return buffer.ToString();
}
Is the retVal the length of the string or some other information? If it's the length of the string, you should assign it to buffer.Length
Avatar of jasonvv

ASKER

Yes it's the length of the return string.

So:

buffer.length = GetFieldValue( filenum.ToString(), buffer.ToString(), buffer.length );
return buffer.ToString();

or

retVal  = GetFieldValue( filenum.ToString(), buffer.ToString(), buffer.length );
buffer.length = retVal
return buffer.ToString();

... This should work?
Avatar of jasonvv

ASKER

I think I found the problem but still need a tip. The documentation for the DLL reads:

======================================================

Return Value:
The length (in bytes) of the string upon successful retrieval of value, otherwise the value returned is less than 0.

Parameters:
chFieldName – Name of field to retrieve data from.  Name is case insensitive.

chReturnValue - Points to the buffer that receives the string.

nMaxSize -  The maximum number of bytes used for the return value buffer size.

======================================================

So my issue is with the return value 'chReturnValue'. It says it's a pointer to a buffer, how do I handle this in C# and get the return string? I am getting the length correctly but I am not sure how to use that to get the string.

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of ptmcomp
ptmcomp
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial