translating vb.net to c# for an external call

g_johnson
g_johnson used Ask the Experts™
on
This VB.Net code calls up a search screen and properly accepts a value into p_sReturnBuffer:

    Public Declare Function DoSearch Lib "searches.dll" ( _
        ByVal SearchID As String, ByVal Server As String, ByVal Database As String, ByVal unknown1 As String, _
        ByVal unknown2 As String, ByVal username As String, ByVal pass As String, ByVal group As String, _
        ByVal RetBuf As String, ByVal RetLen As Long, ByVal Filter As String) As Boolean

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ShopOrderBegSearch()
    End Sub
    Private Sub ShopOrderBegSearch()
        Dim p_lReturnBufferLen As Long = 255
        Dim p_sReturnBuffer As String = Space(p_lReturnBufferLen)
        Dim p_bRtnVal As Boolean
        Dim p_sFilter As String = ""
        Dim p_sFilterrtn As String
        Dim p_SSearchID As String = "SHOP-ORDER-NO"
        Dim p_lFilterrtnLen As Long = 8

        p_bRtnVal = DoSearch(p_SSearchID, "WIN-G36452O7", "783", "", "", "", "", "", p_sReturnBuffer, p_lReturnBufferLen, p_sFilter)
        p_sFilterrtn = Strings.Left(p_sReturnBuffer, p_lFilterrtnLen)
        If p_bRtnVal Then
            MessageBox.Show(Trim(p_sFilterrtn))
        End If
    End Sub

Open in new window

       



This is what I think is the equivalent C# code, which does indeed call up the search screen, but p_sReturnBuffer does not get changed from the original 255 character string to the correct return value.  Any idea what I am doing wrong?


[DllImport("searches.dll")]
        public static extern bool DoSearch(string SearchID, string Server, string Database, string unknown1, string unknown2, string username, string pass, string group, string RetBuf, long RetLen, string Filter);


       private void attempttodosearch()
        {

            long p_lReturnBufferLen = 255;
            string p_sReturnBuffer = new String(' ',255);//Strings.Space(p_lReturnBufferLen);
            bool p_bRtnVal = false;
            string p_sFilter = "";
            string p_sFilterrtn = null;
            string p_SSearchID = "SHOP-ORDER-NO";
            long p_lFilterrtnLen = 8;

            p_bRtnVal = DoSearch(p_SSearchID, "WIN-G36452O7", "783", "", "", "", "", "", p_sReturnBuffer, p_lReturnBufferLen,
            p_sFilter);
            p_sFilterrtn = p_sReturnBuffer.Substring(0, (int)p_lFilterrtnLen);// Strings.Left(p_sReturnBuffer, p_lFilterrtnLen);
            if (p_bRtnVal)
            {
                MessageBox.Show(p_sFilterrtn.Trim());
            }

            //string rBuffer = new String(' ', 255);
            //long bLen = 255;
            //string sFilter = "";

            //bool retVal = DoSearch("SHOP-ORDER-NO", this._Server, this._Database, "", "", "", "", "", rBuffer, bLen, sFilter);
            //if (retVal)
            //{
            //    string result = rBuffer.Substring(0, Convert.ToInt32(bLen));
            //    MessageBox.Show(result);
            //}
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Don't specify a space on the string; e.g. -
[DllImport("searches.dll")]
        public static extern bool DoSearch(string SearchID, string Server, string Database, string unknown1, string unknown2, string username, string pass, string group, string RetBuf, long RetLen, string Filter);


       private void attempttodosearch()
        {

            long p_lReturnBufferLen = 255;
            string p_sReturnBuffer = string.Empty;
            bool p_bRtnVal = false;
            string p_sFilter = "";
            string p_sFilterrtn = null;
            string p_SSearchID = "SHOP-ORDER-NO";
            long p_lFilterrtnLen = 8;

            p_bRtnVal = DoSearch(p_SSearchID, "WIN-G36452O7", "783", "", "", "", "", "", p_sReturnBuffer, p_lReturnBufferLen,
            p_sFilter);
            p_sFilterrtn = p_sReturnBuffer.Substring(0, (int)p_lFilterrtnLen);// Strings.Left(p_sReturnBuffer, p_lFilterrtnLen);
            if (p_bRtnVal)
            {
                MessageBox.Show(p_sFilterrtn.Trim());
            }

            //string rBuffer = new String(' ', 255);
            //long bLen = 255;
            //string sFilter = "";

            //bool retVal = DoSearch("SHOP-ORDER-NO", this._Server, this._Database, "", "", "", "", "", rBuffer, bLen, sFilter);
            //if (retVal)
            //{
            //    string result = rBuffer.Substring(0, Convert.ToInt32(bLen));
            //    MessageBox.Show(result);
            //}
        }

Open in new window

-saige-

Author

Commented:
Hi saige -- that breaks the DLL as in getting a "stopped working" message.

Author

Commented:
We found out that the error is actually a warning that can be ignored.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial