Paul Elliott

Hi,

I'm now able to pass strings ( even with embedded nulls ) back & forth from

C# to a PB DLL as long as they are parameters. However I can't return a

null-embedded string as a return from the PB DLL. It comes back as an

ASCIIZ string & gets cut off at the first null. I added

[return:MarshalAs(UnmanagedType.AnsiBStr)]

which the help file says is supposed to solve the problem but it makes no

difference.

If I change to returning an IntPtr, I can use SysStringByteLen to see to real

length of the string from PB and handle it that way. But I shouldn't have to.

What am I doing wrong

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

This is the C# code I'm using :

private void button2_Click(object sender, EventArgs e)
{
string StrParm;
string StrBack;
IntPtr backptr;
int backlen;

StreamWriter txtOut;
string pth;
string wrk;

StrParm = "paul.txt\0wow";
//backptr = trm_String(ref StrParm);
//backlen = SysStringByteLen(backptr);
StrBack = "**";
StrBack = trm_String( ref StrParm);
label2.Text = "StrParm =" + StrParm + "<<";
//StrBack = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(backptr, backlen);
label3.Text = "StrBack =" + StrBack + "<< len =" + StrBack.Length.ToString();

pth = @"E:\trmopn.txt";
txtOut = File.AppendText(pth);
wrk = "StrBack len =" + StrBack.Length.ToString() + " >>" + StrBack + "<<";
txtOut.WriteLine(wrk);

txtOut.Close();
}

This is the declare for the PB function:
DECLARE FUNCTION trm_String ALIAS "trm_String" ( STRING) AS STRING

and the actual functions :
FUNCTION trm_String( StrParm AS STRING ) EXPORT AS STRING
LOCAL e AS LONG
LOCAL adr AS LONG
LOCAL str2 AS STRING
LOCAL str AS STRING
LOCAL asp AS ASCIIZ PTR
OPEN "e:\trmopn.txt" FOR APPEND AS #1
ERRCLEAR
PRINT #1, ""
PRINT #1, DATE$;" "; TIME$
PRINT #1, "trm_String"
PRINT #1, "len StrParm ="; LEN(StrParm)
IF LEN(StrParm) < 50 THEN
PRINT #1, "StrParm ="; StrParm
ELSE
PRINT #1, "* StrParm ="; LEFT$(StrParm, 12)
END IF
asp = STRPTR(StrParm)
str = @asp
PRINT #1, "from Asciiz Ptr ="; str
e = ERR
PRINT #1, "err = "; e
CLOSE #1
StrParm = "wowbag.txt"
str2 = str & CHR$(0) & "wowee"
FUNCTION = str2
END FUNCTION



Re: Common Language Runtime trouble returning string w/embedded nulls from PB to C#

nobugz

If SysStringByteLen() works then UnmanagedType.AnsiBStr is incorrect. Use BStr or VBByRefStr.





Re: Common Language Runtime trouble returning string w/embedded nulls from PB to C#

Yi Zhang [MSFT]

Hi,

You are doing nothing wrong here. We've confirmed that this is a product issue. It will be fixed in the next major release cycle, but the dates are not finalized yet. For now, please use the workaround of IntPtr & SysStringByteLen.

Sorry for the inconvenience and thank you for your understanding.

--

Yi Zhang, Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.





Re: Common Language Runtime trouble returning string w/embedded nulls from PB to C#

Paul Elliott

Thank you. I'm glad that someone else has confirmed the problem and that a permanent solution will be provided.