Announcement

Collapse
No announcement yet.

.net c# webservice to PB dll

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    .net c# webservice to PB dll

    Hi

    I hope someone can help me solve this riddle with c#.
    I'm trying to write a webservice that calls a PB dll (v8.04).

    My dll (besides a clean LibMain) is this:

    Code:
    FUNCTION Test1 ALIAS "TEST1" ( ProjectID AS LONG, sVar AS STRING ) EXPORT AS LONG
    
        FUNCTION = LEN( sVar )
    
    END FUNCTION  
    
    FUNCTION Test2 ALIAS "TEST2" ( ProjectID AS LONG, sVar AS STRING * 255 ) EXPORT AS LONG
    
        FUNCTION = LEN(TRIM$( sVar ))
    
    END FUNCTION
    I'm trying to find out how to pass a string the proper way.

    In my c# webservice, I have the following declarations and webmethod:

    Code:
        [DllImport("test.dll", EntryPoint = "TEST1", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern int test1(
            int projectid,
            [MarshalAs(UnmanagedType.AnsiBStr,SizeConst=255)]
            string sVar
        );
    
        [DllImport("test.dll", EntryPoint = "TEST2", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern int test2(
            int projectid,
            [MarshalAs(UnmanagedType.AnsiBStr,SizeConst=255)]
            string sVar
        );
    
        [WebMethod]
        public int RunTest1(string sVar)
        {
            try
            {
                return test1(1, sVar);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    
        [WebMethod]
        public int RunTest2(string sVar)
        {
            try
            {
                return test2(1, sVar);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    What I get back is always 255, even I just enter a sVar of just 4 characters. Extracting on Chr$(0) doesn't help either.

    I've tried playing with leaving the SizeConst in the webdeclaration and AnsiBStr and I just cannot figure out what I'm doing wrong here.



    What I want to archieve in the end is how to pass a pointer (of a string or a struct):

    Additional function in PB
    Code:
    FUNCTION Test3 ALIAS "TEST3" ( ProjectID AS LONG, BYVAL  sVar AS STRING PTR ) EXPORT AS LONG
    
        FUNCTION = LEN(TRIM$( @sVar ))
    
    END FUNCTION
    Additional stuff in the webservice:
    Code:
    [DllImport("test.dll", EntryPoint = "TEST3", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern int test3(
            IntPtr sVar
        );
    
    
    public int RunTest3(string sQuestion)
        {
            try
            {
                IntPtr s = Marshal.StringToHGlobalAnsi(sQuestion);
    
                int result = test3(1, ref s);
    
                Marshal.FreeHGlobal(s);
    
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    This doesn't work at all.. At least I cannot get it to work.


    Hope someone can give me tip on where to go with this.

    Many regards

    Jeroen Brouwers

    #2
    The StringToHGlobalAnsi method might be returning a Ptr to an ASCIIZ string rather than a "STRING" string. (OLE string).

    In which case you just have to change the procedure header in your PB function to either "[BYREF] sz AS ASCIIZ" or "BYVAL psz AS ASCIIZ PTR"

    Worth a try anyway.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎