Thread: C++ .Net DLL return values

  1. #1
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21

    Unhappy C++ .Net DLL return values

    I've got a lib that I'm trying to get running except I don't know how to return a string. I'm using VB .Net and C++ .Net. The VB project is a Windows Application and the C++ is a Managed C++ Class Library. I have found the wizard to create functions that will be accessible to my VB program. Now when I try to put in my own return value (char * or char []) VB tells me that my function in the DLL is returning a value that it cannot resolve. Anyone know how to get around this because I have a lot of data that will be going through my DLL to my VB project and I kinda need to be able to send it a string and not just character by character. Thanks.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    not sure about the world of .NET but before that, a VB string was much different than a char *. There was a conversion function for getting ASCII chars from a VB string.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    The problem with that is that I'm have the string in C++ and I'm trying to get it to print out in VB.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ah, well I think there is a function to go the other way too. I'll try to look for it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wait a sec.... doesn't VB accept BSTR? if that's the case you should be able to do the conversion in C++ and pass a BSTR.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why not return a void* to a null terminated string? Anyway, is it safe to simply return a chunk of memory that way? Why not use the windows clipboard for the transfer?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    Originally posted by Sebastiani
    Why not return a void* to a null terminated string? Anyway, is it safe to simply return a chunk of memory that way? Why not use the windows clipboard for the transfer?
    Well to start off I have never used the windows clipboard in a program so I don't know how or the syntax.

    Secondly for the void * ... once again VB doesn't like it as a return type.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm definitely thinking BSTR because that's what they used for COM
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    One of two things is wrong with BSTR. Either I'm not using it properly or else VB .Net doesn't like it. It could be either problem because I have no clue how to use it. I know I'm getting the string into the BSTR fine but VB doesn't like getting it. BTW I'm sending the reutrn value directly into a MsgBox so I don't know if that has anything to do with it. But I do know that my function that returns the BTR ... yeah VB doesn't like that as a return type.

    I'm getting the feeling that I'm not going to like .Net stuff that much any more.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you are using .NET on both ends, why not simply use System.String as parameter ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Not sure about this, but I think BSTR may contain wide string characters.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes it does, and I think the first two bytes are the length. It's an odd type but I believe it's what VB uses
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    Originally posted by nvoigt
    If you are using .NET on both ends, why not simply use System.String as parameter ?

    Sounds like a good idea. There's only one problem with it ... I have tried to impliment it but I honestally don't know how to from inside VC++ .Net. Think you could help me impliment it or point me to a site that could.

  14. #14
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    SUCCESS!!!

    Thank you all who gave me ideas to get through this. This is what I ended up using and it worked.

    Code:
    C++ code
    
    String * fred(void)
    {
      String * fred2 = "Hello World";
      return fred2;
    }
    
    
    VB code
    
    Dim alpha as fredDLL.main
    alpha = new fredDLL.main()
    
    MsgBox(alpha.fred())
    I know it doesn't look like much, but it's taken me about a week to figure out how to transfer string from VC++ to VB in .Net so to save you the hassle here it is.

  15. #15
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Can someone tell me is that
    Code:
    String * fred2 = "Hello World";
    declaring a STL string? The kind you get from
    Code:
    #include <string>
    Or is that some other type of string?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM