• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

C# string to a PB-dll??

Started by Heinz Grandjean, November 05, 2013, 05:57:07 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Edwin Knoppert

I'll prepare a better example from this and place it on my website later on.
Goal is to detect correct release of the BSTR and provide an in and out BSTR and unicode BSTR param.

Edwin Knoppert

#16
Here is the test already, it's not finished but i wanted to show you the results.
When retrieved incorrectly/unhandled it clear shows the PB dll claims new memory (see ouput).
When treated as BSTR it will discard the return variable correctly..

c#

using System;
using System.Runtime.InteropServices;

namespace BSTR
{
    public class MakeCall
    {
        [DllImport("test.dll", EntryPoint = "TEST")]
        [return: MarshalAs(UnmanagedType.SysUInt)]
        public static extern IntPtr TestUInt();

        [DllImport("test.dll", EntryPoint = "TEST")]
        [return: MarshalAs(UnmanagedType.AnsiBStr)]
        public static extern string Test();

        public void runtest()
        {
            Console.WriteLine("Return pointers:");
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(TestUInt());
            }

            Console.WriteLine("\r\nReturn BSTR:");
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(Test());
            }
        }
    }
}


PB dll

Function TEST () Export As String
    Local testvar As String
    testvar = "4556"
    Function = Format$( StrPtr( testvar ) )
End Function


Output

Return pointers:
75761660
75762948
75763028
75761852

Return BSTR:
75761636
75761636
75761636
75761636

Edwin Knoppert

I am having trouble returning a unicode string:

        [DllImport("test.dll", EntryPoint = "TESTW")]
        [return: MarshalAs(UnmanagedType.BStr)]
        public static extern string TestW();

            Console.WriteLine("\r\nReturn Unicode BSTR:");
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(TestW());
            }



Function TESTW () Export As WString
    Local testvar As WString
    testvar = "dummy"
    testvar = Format$( StrPtr( testvar ) )
    Function = testvar
End Function


The 2nd call will destroy the previous one, may be a garbage collection thing, not sure..
?

Return pointers:
78668900
78668876
78617628
78669092

Return Unicode BSTR:
78617708
78669116
78617708
78669116

Return BSTR:
78617708
78617708
78617708
78617708


Paul Elliott

I said it was crude but it was my test-bed for trying various routines.
But use the routines without Marshall. I've been able to pass PB strings back and forth without
any problems ( or at least none I've noticed and it hasn't crashed yet ).

Comment out the trm inc and see which routines need it. Then just comment them out.
I started out testing straight to the TRM.dll and then moved on to my own version of the
string routines so that I could see more detail of what worked and what didn't.


Edwin Knoppert

Here is the byref as requested..

        [DllImport("test.dll", EntryPoint = "TESTBYREFSTRING")]
        [return: MarshalAs(UnmanagedType.AnsiBStr)]       
        public static extern string TestByRefString([MarshalAs(UnmanagedType.AnsiBStr)] ref string s);

            string s = "5678";
            Console.WriteLine(TestByRefString(ref s));
            Console.WriteLine("s is now: {0}", s);



Function TESTBYREFSTRING( ByRef s As String ) Export As String
    Function = s
    s = Time$
End Function


You can not intermix with asciiz of course!

Edwin Knoppert

Quote
I started out testing straight to the TRM.dll and then moved on to my own version of the
string routines so that I could see more detail of what worked and what didn't.

All my examples here work except that unicode version is behaving odd, maybe someone has an idea what is going on.
Doesn't have to be wrong per se.

Heinz Grandjean

Paul,
I have now tested directly your example.
Everything ( that has  not to be commented out...) works fine.
But the call to

StrBack = trm_AsciiZ(StrParm);

inside the method button3_Click doesn't work!!!
The program crashes.
It is like Edwin has said before...
I use VS2012.

Heinz Grandjean



Edwin Knoppert

For trm i guess you need a lpstr (or simular name) not bstr.
A bstr is a specific breed.