如何解决vc做的动态连接库中字符串的问题(其他如vb,delphi向其传递字符串)希望从其他语言中传递一个字符串,然后由vc的动态连接库进行处理

解决方案 »

  1.   

    short WINAPI StringArgs(BSTR *pbstrArg1, 
        BSTR *pbstrArg2, short cch)
    {
        BSTR *pbstrTemp;    // Return error code if requested characters 
        // less than zero, or input string is unassigned 
        // or has too few characters.
        // Use ByteLen since string is not unicode
        if (cch < 0 || *pbstrArg1 == NULL || 
            (short)SysStringByteLen(*pbstrArg1) < cch)
            return -1;            if (*pbstrArg2 == NULL) 
        {     // String is unassigned; 
            // we can allocate a new one.
            // Use ByteLen since string is not unicode
            *pbstrArg2 = SysAllocStringByteLen((LPSTR)*pbstrArg1, cch);
            if (*pbstrArg2 == NULL)
                return -2;
        }
        else 
        {     // Argument string is already assigned; 
            // we must reallocate.
            *pbstrTemp = SysAllocStringByteLen((LPSTR)*pbstrArg1, cch);
            // Did it fail?
            if (pbstrTemp == NULL)
                return -3;        SysFreeString(*pbstrArg2);
            *pbstrArg2 = *pbstrTemp;
        }
            
        return 0;
    }
    Declare Function StringArgs Lib "debug\ADVDLL.DLL" _
        (inpStr As String, outStr As String, ByVal n As Integer) As IntegerSub StringArgsTest()
        Dim newStr As String
        Dim x As Boolean    ''' First code path
        x = StringArgs("abracadabra", newStr, 5)
        MsgBox x & ":" & newStr    ''' Second code path
        x = StringArgs("abracadabra", newStr, 4)
        MsgBox x & ":" & newStr
    End Sub
      

  2.   

    #include <windows.h>      void __stdcall DisplayStringByVal(LPCSTR pszString)
          {
             //pszString is a pointer to a string
             MessageBox(NULL, pszString, "Display String ByVal",
                       MB_OK | MB_ICONINFORMATION);
          }      void __stdcall DisplayStringByRef(LPCSTR* ppszString)
          {
             //ppszSting is a pointer to a pointer to a string
             MessageBox(NULL, *ppszString, "Display String ByRef",
                       MB_OK | MB_ICONINFORMATION);
          }         void __stdcall FillString(LPSTR pszString, LONG cSize)
          {
             // Create a temp buffer with our string
             char buffer[] = "Hello from the C DLL!";         // Copy our temp string to pszString
             // but check the size to make sure we have a buffer
             // big enough to hold the entire string.
             if (cSize > strlen(buffer))
                strcpy(pszString, buffer);
          }      int __stdcall InStrRev(LPCSTR pszString, short iChar)
          {
             // This function is similar to Visual Basic's InStr function
             // except that it searches for the given ASCII character from
             // right to left, returning the character position of the
             // last occurrence (rather than the first) of the character
             // in the string.
             char* pszTmp;
             int nRet = 0;         // Scan for iChar in pszString backwards
             pszTmp = strrchr(pszString, (int)iChar);
             if(pszTmp != NULL)
                nRet = pszTmp - pszString + 1;         return nRet;
          }
     
    StrSamp.def:
      LIBRARY StrSamp
          DESCRIPTION 'Microsoft KB Sample DLL'
          EXPORTS
             DisplayStringByVal
             DisplayStringByRef
             FillString
             InStrRev
     
          Option Explicit      Private Declare Sub DisplayStringByRef Lib "StrSamp.dll" _
             (sMyString As String)
          Private Declare Sub DisplayStringByVal Lib "StrSamp.dll" _
             (ByVal sMyString As String)
          Private Declare Sub FillString Lib "StrSamp.dll" _
             (ByVal sMyString As String, ByVal cBufferSize As Long)
          Private Declare Function InStrRev Lib "StrSamp.dll" _
             (ByVal sMyString As String, ByVal iChar As Integer) _
             As Long
      Private Sub Command1_Click()
             Dim sTestString1 As String
             Dim sTestString2 As String         sTestString1 = "This is my string passed to the dll by value."
             DisplayStringByVal sTestString1         sTestString2 = "This is my string passed to the dll by reference."
             DisplayStringByRef sTestString2      End Sub      Private Sub Command2_Click()
             Dim sFillTest As String         sFillTest = Space$(260)
             FillString sFillTest, 260
             MsgBox Trim$(sFillTest), vbInformation, "Fill String"
          End Sub      Private Sub Command3_Click()
             Dim sPathString As String
             Dim sMsg As String
             Dim lCharPosition As Long         sPathString = "C:\My Documents\Temp\Item.txt"
             lCharPosition = InStrRev(sPathString, Asc("\"))         If CBool(lCharPosition) Then
                sMsg = "The file '" & Mid$(sPathString, lCharPosition + 1)
                sMsg = sMsg & "' is at this location:" & vbCrLf & vbCrLf
                sMsg = sMsg & Left$(sPathString, lCharPosition - 1)
                MsgBox sMsg, vbInformation, "InStrRev"
             Else
                MsgBox "Cannot find '/' in " & sPathString, vbCritical
            End If      End Sub