VB-Error when calling c++ DLL that returns a string 
Author Message
 VB-Error when calling c++ DLL that returns a string

When Calling this Dll from VB it says that I caused an illegal action
and VB is terminated
Whenever I make a dll that returns an integer I have no probs
for example if I would return Decimaal there will be no errors, but
that's not what I need :(

Any help would be welcome

This is the code of my C++ DLL

// TP-Software (Testing Project)

#include <string>
using namespace std;

string _stdcall DecToBin(int Decimaal)
{
 string BinUitvoer;
    int Bit[8], I,T,temp;

 for (I = 7; I >= 0; I--)
 {
  temp = 2;
  for(T = 1; T <= I;T++)
  {
   temp = temp * 2;
  }

  Bit[I + 1] = temp;
 }

 for (I = 8; I > 0; I--)
 {

  temp = Decimaal / Bit[I];

  if ( temp = 1)
  {
   BinUitvoer = BinUitvoer + "1";
  }
  else
  {
   BinUitvoer = BinUitvoer + "0";
        }
 }

 return BinUitvoer;

Quote:
}



Mon, 19 Aug 2002 03:00:00 GMT  
 VB-Error when calling c++ DLL that returns a string
Try this somewhat old C DLL code, it once worked for me.

#include <windows.h>

int WINAPI DllMain (HINSTANCE hInst, DWORD ul_reason_for_call, LPVOID
lpReserverd)
{
    return 1;

Quote:
}

// This next one is the one you want.

__declspec (dllexport) LPSTR CALLBACK strData(void)
{
 return("This is a string from C");

Quote:
}

__declspec (dllexport) LONG CALLBACK strLength(LPSTR src)
{
 return strlen(src);

Quote:
}

__declspec (dllexport) VOID CALLBACK strDecode(LPSTR src, LPSTR dest, long
len)
{
 strncpy(dest, src, len);
Quote:
}



Quote:
> When Calling this Dll from VB it says that I caused an illegal action
> and VB is terminated
> Whenever I make a dll that returns an integer I have no probs
> for example if I would return Decimaal there will be no errors, but
> that's not what I need :(

> Any help would be welcome

> This is the code of my C++ DLL

> // TP-Software (Testing Project)

> #include <string>
> using namespace std;

> string _stdcall DecToBin(int Decimaal)
> {
>  string BinUitvoer;
>     int Bit[8], I,T,temp;

>  for (I = 7; I >= 0; I--)
>  {
>   temp = 2;
>   for(T = 1; T <= I;T++)
>   {
>    temp = temp * 2;
>   }

>   Bit[I + 1] = temp;
>  }

>  for (I = 8; I > 0; I--)
>  {

>   temp = Decimaal / Bit[I];

>   if ( temp = 1)
>   {
>    BinUitvoer = BinUitvoer + "1";
>   }
>   else
>   {
>    BinUitvoer = BinUitvoer + "0";
>         }
>  }

>  return BinUitvoer;

> }



Mon, 19 Aug 2002 03:00:00 GMT  
 VB-Error when calling c++ DLL that returns a string
  >
  > When Calling this Dll from VB it says that I caused an illegal action
  > and VB is terminated
  > Whenever I make a dll that returns an integer I have no probs
  > for example if I would return Decimaal there will be no errors, but
  > that's not what I need :(
  >
  ...

I have never gotten a string value as *function result* back from a DLL.    
This seems the correct way to treat strings between VB and a DLL:

In a VB module declare the following:

'Yes, the strings must be declared 'ByVal' and are treated as if 'ByRef'.

Public Declare Function handle_line Lib "<DLLNAME>.dll" _

      ByVal lpInput As String, _
      ByVal lpOutput As String) As Integer

  Use somewhere:

      Dim strInput   As String
      Dim strOutput  As String
      Dim iLen       As Integer

      strInput = "this is the input string"

      ' do not forget to initialize this string
      strOutput = Space(1024)

      iLen = handle_line(strInput, strOutput)
      strOutput = Left(strOutput, iLen)

  In your DLL:

  /* this routine only copies over spaces, and does not Trim */
  long        CopyBack(char *target, char *source, long iMaxResult)
  {
      long        lResult = 0;

      while ((lResult < iMaxResult) && (*source != '\0') &&
                                       (*target == ' '))
      {
          *target++ = *source++;
          lResult++;
      }
      return (lResult);
  }

  DllExport WORD WINAPI handle_line(LPSTR strInput, LPSTR strOutput)
  {
      int         iResult;
      long        iLen;
      char       *lst_err;
      char        cbuff[1024];

      /* the length of the original string */
      iLen = strlen(strOutput);

      /* fill cbuff with something */
      strcpy(cbuff, "Hello World");

      /* this is the length of the returned string */
      iResult = strlen(cbuff);

      CopyBack(strOutput, cbuff, iLen);

      /* return the correct length */
      return (iResult);
  }

--
Posted via CNET Help.com
http://www.help.com/



Tue, 20 Aug 2002 03:00:00 GMT  
 VB-Error when calling c++ DLL that returns a string
C Strings and VB strings have little in common.  As others have suggested,
one way to handle this is to pass in a pre-sized buffer that your C function
can fill in.

A cleaner way is to use what VB and C do have in common: OLE BSTRs.  Use the
Sys...String API to create an Ansi BSTR from a C LPSTR, and return that as
the function result.  VB will happily accept it and convert it to a Unicode
string on return.

BSTR __stdcall DecToBin(int Decimal);
...
...
return SysAllocStringByteLen(&myLPSTR, strlen(myLPSTR))

--
     Jim Mack
     MicroDexterity, Inc
     www.microdexterity.com


Quote:
> When Calling this Dll from VB it says that I caused an illegal action
> and VB is terminated
> Whenever I make a dll that returns an integer I have no probs
> for example if I would return Decimaal there will be no errors, but
> that's not what I need :(

> Any help would be welcome

> This is the code of my C++ DLL

> // TP-Software (Testing Project)

> #include <string>
> using namespace std;

> string _stdcall DecToBin(int Decimaal)
> {
>  string BinUitvoer;
>     int Bit[8], I,T,temp;

>  for (I = 7; I >= 0; I--)
>  {
>   temp = 2;
>   for(T = 1; T <= I;T++)
>   {
>    temp = temp * 2;
>   }

>   Bit[I + 1] = temp;
>  }

>  for (I = 8; I > 0; I--)
>  {

>   temp = Decimaal / Bit[I];

>   if ( temp = 1)
>   {
>    BinUitvoer = BinUitvoer + "1";
>   }
>   else
>   {
>    BinUitvoer = BinUitvoer + "0";
>         }
>  }

>  return BinUitvoer;

> }



Tue, 20 Aug 2002 03:00:00 GMT  
 VB-Error when calling c++ DLL that returns a string
Thnx H Salomons :-)
Boomer.
Quote:


>   > When Calling this Dll from VB it says that I caused an illegal action
>   > and VB is terminated
>   > Whenever I make a dll that returns an integer I have no probs
>   > for example if I would return Decimaal there will be no errors, but
>   > that's not what I need :(

>   ...

> I have never gotten a string value as *function result* back from a DLL.
> This seems the correct way to treat strings between VB and a DLL:

> In a VB module declare the following:

> 'Yes, the strings must be declared 'ByVal' and are treated as if 'ByRef'.

> Public Declare Function handle_line Lib "<DLLNAME>.dll" _

>       ByVal lpInput As String, _
>       ByVal lpOutput As String) As Integer

>   Use somewhere:

>       Dim strInput   As String
>       Dim strOutput  As String
>       Dim iLen       As Integer

>       strInput = "this is the input string"

>       ' do not forget to initialize this string
>       strOutput = Space(1024)

>       iLen = handle_line(strInput, strOutput)
>       strOutput = Left(strOutput, iLen)

>   In your DLL:

>   /* this routine only copies over spaces, and does not Trim */
>   long        CopyBack(char *target, char *source, long iMaxResult)
>   {
>       long        lResult = 0;

>       while ((lResult < iMaxResult) && (*source != '\0') &&
>                                        (*target == ' '))
>       {
>           *target++ = *source++;
>           lResult++;
>       }
>       return (lResult);
>   }

>   DllExport WORD WINAPI handle_line(LPSTR strInput, LPSTR strOutput)
>   {
>       int         iResult;
>       long        iLen;
>       char       *lst_err;
>       char        cbuff[1024];

>       /* the length of the original string */
>       iLen = strlen(strOutput);

>       /* fill cbuff with something */
>       strcpy(cbuff, "Hello World");

>       /* this is the length of the returned string */
>       iResult = strlen(cbuff);

>       CopyBack(strOutput, cbuff, iLen);

>       /* return the correct length */
>       return (iResult);
>   }

> --
> Posted via CNET Help.com
> http://www.help.com/



Tue, 20 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Help How to get MS C++ DLL function to return string to VB

2. Returning strings from VB COM components to C++ Isapi DLL

3. VB calling C++ DLL returning an array??

4. Calling C++ DLLs from VB with string params

5. Calling C++ DLLs from VB with string params

6. VB 5.0 calling C++ 5.0 DLL that calls winsock.dll

7. returning a string from a c dll to a VB calling program

8. Returning strings from C DLL functions called by VB

9. VB-ActiveX-DLL access violation error when call from C++

10. How does a C++ DLL return a string?

11. Help with returning a sz string from a C++ DLL to VB5

12. Holding a pointer to a C++ object being returned from a DLL call

 

 
Powered by phpBB® Forum Software