Author Topic: C# string to a PB-dll??  (Read 7330 times)

0 Members and 1 Guest are viewing this topic.

Offline Edwin Knoppert

  • Hero Member
  • *****
  • Posts: 554
  • Gender: Male
    • Hellobasic.com
Re: C# string to a PB-dll??
« Reply #15 on: November 06, 2013, 02:38:54 PM »
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.

Offline Edwin Knoppert

  • Hero Member
  • *****
  • Posts: 554
  • Gender: Male
    • Hellobasic.com
Re: C# string to a PB-dll??
« Reply #16 on: November 06, 2013, 04:56:04 PM »
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#
Code: [Select]
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
Code: [Select]
Function TEST () Export As String
    Local testvar As String
    testvar = "4556"
    Function = Format$( StrPtr( testvar ) )
End Function

Output
Code: [Select]
Return pointers:
75761660
75762948
75763028
75761852

Return BSTR:
75761636
75761636
75761636
75761636
« Last Edit: November 06, 2013, 05:00:18 PM by Edwin Knoppert »

Offline Edwin Knoppert

  • Hero Member
  • *****
  • Posts: 554
  • Gender: Male
    • Hellobasic.com
Re: C# string to a PB-dll??
« Reply #17 on: November 06, 2013, 05:19:04 PM »
I am having trouble returning a unicode string:
Code: [Select]
        [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());
            }

Code: [Select]
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..
?
Code: [Select]
Return pointers:
78668900
78668876
78617628
78669092

Return Unicode BSTR:
78617708
78669116
78617708
78669116

Return BSTR:
78617708
78617708
78617708
78617708

Offline Paul Elliott

  • Full Member
  • ***
  • Posts: 179
Re: C# string to a PB-dll??
« Reply #18 on: November 06, 2013, 09:51:13 PM »
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.


Offline Edwin Knoppert

  • Hero Member
  • *****
  • Posts: 554
  • Gender: Male
    • Hellobasic.com
Re: C# string to a PB-dll??
« Reply #19 on: November 06, 2013, 09:51:49 PM »
Here is the byref as requested..
Code: [Select]
        [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);

Code: [Select]
Function TESTBYREFSTRING( ByRef s As String ) Export As String
    Function = s
    s = Time$
End Function

You can not intermix with asciiz of course!

Offline Edwin Knoppert

  • Hero Member
  • *****
  • Posts: 554
  • Gender: Male
    • Hellobasic.com
Re: C# string to a PB-dll??
« Reply #20 on: November 06, 2013, 09:53:59 PM »
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.

Offline Heinz Grandjean

  • Newbie
  • *
  • Posts: 22
Re: C# string to a PB-dll??
« Reply #21 on: November 08, 2013, 02:04:57 PM »
Paul,
I have now tested directly your example.
Everything ( that has  not to be commented out...) works fine.
But the call to
Code: [Select]
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



Offline Edwin Knoppert

  • Hero Member
  • *****
  • Posts: 554
  • Gender: Male
    • Hellobasic.com
Re: C# string to a PB-dll??
« Reply #22 on: November 08, 2013, 02:54:52 PM »
For trm i guess you need a lpstr (or simular name) not bstr.
A bstr is a specific breed.