简体   繁体   English

C ++转VB.Net IntPtr字符串

[英]c++ to VB.Net IntPtr Strings

Alright so I have this code, and I pass it to an unmanaged dll, to which I only know the exports, and have some sample code. 好了,所以我有了这段代码,并将其传递给一个非托管的dll,我只知道该dll的导出,并提供了一些示例代码。 I'm getting back the correct string, but it's followed by garbage bytes. 我找回了正确的字符串,但是后面跟着垃圾字节。

I'm basically translating code verbatim from a c++ example program that doesn't have this issue. 我基本上是从不存在此问题的c ++示例程序逐字翻译代码。 I'm assume there is something fundamental I am missing here, so if anyone could tell me what that is, I'd appreciate it. 我认为这里缺少一些基本知识,因此,如果有人可以告诉我那是什么,我将不胜感激。

Example C++ Code 示例C ++代码

void CDUKPT_TESTDlg::OnButton4() 
{
    // TODO: Add your control notification handler code here
    unsigned char dataout[1024],tmp[1024],ksn[20],keyval[20];
    int nRet,len;
    memset(dataout,0,sizeof(dataout));
    memset(ksn,0,sizeof(ksn));
    memset(keyval,0,sizeof(keyval));
    memset(tmp,0,sizeof(tmp));
    UpdateData(TRUE);

    two_one((unsigned char *)m_strCURKSN.GetBuffer(m_strCURKSN.GetLength()),m_strCURKSN.GetLength(),ksn);
    two_one((unsigned char *)m_strMACK.GetBuffer(m_strMACK.GetLength()),m_strMACK.GetLength(),keyval);
    two_one((unsigned char *)m_EncryptDat.GetBuffer(m_EncryptDat.GetLength()),m_EncryptDat.GetLength(),dataout);

    len=m_EncryptDat.GetLength()/2;
    //extern int __stdcall ExtractDat(unsigned char *input,
    //unsigned short len,unsigned char *output,unsigned char *key,
    //unsigned char *ksn);
    nRet=ExtractDat(dataout,len,tmp,keyval,ksn); //External Call
    //Good string+bad trailing data comes back in tmp
    m_Result=tmp;
    UpdateData(FALSE);
}

This code spits out this ܉Òdÿo 这段代码吐出了这个܉Òdÿo

Here is my VB.Net Code 这是我的VB.Net代码

Public Function Encrypt(ByVal inp As String) As String
    Dim tmpSB As New StringBuilder
    Dim i As Integer
    Dim tKsn As Char() = TwoOne(StrCurKsn)
    For i = tKsn.Length To 19
        tKsn = tKsn + Chr(0)
    Next
    Dim tMack As Char() = TwoOne(StrMack)
    For i = tMack.Length To 19
        tMack = tMack + Chr(0)
    Next
    Dim tEnc As Char() = TwoOne(inp)
    For i = tEnc.Length To 1023
        tEnc = tEnc + Chr(0)
    Next
    Dim len As Integer = tEnc.Length / 2

    Dim tmpStr(1023) As Char
    Array.Clear(tmpStr, 0, 1024)
    Dim tmpPtr = Marshal.StringToHGlobalAnsi(tmpStr)

    Dim nRet = ExtractDat(tEnc, len, tmpPtr, tMack, tKsn)

    tmpStr = Marshal.PtrToStringAnsi(tmpPtr)
    Dim tsl = tmpStr.Length
    Encrypt = tmpStr
End Function

This code spits this out 此代码将其吐出

܉Òdÿo ålUäÙålUäÙålUäÙålUäÙålUäÙålUäÙålUäÙ

So I get the right string, but it's followed by a repeating string of garbage characters. 所以我得到了正确的字符串,但是后面跟着重复的垃圾字符字符串。 I'm hoping I've done something blatantly wrong here, but I've tried pulling the data as bytes, and chars, and converting in many different methods, and I can't seem to get rid of those characters...Also, ExtractDat doesn't return the length of the string(not a problem, as it's not supposed to, which is really annoying). 我希望我在这里做了一些公然的错误,但是我尝试将数据作为字节和char进行提取,并以许多不同的方法进行转换,但我似乎无法摆脱这些字符...也, ExtractDat不会返回字符串的长度(这不是问题,因为它不应该这样做,这确实很烦人)。

事实证明该dll不好,因此在我从供应商那里获得了新的编译之后,它似乎可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM