Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Wrong DLL output

Wrong DLL output

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++visual-studiodebugging
20 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Cedric Moonen

    I am far from being a VB expert, but shouldn't you pass the integer ByVal instead of ByRef ? Did you try to debug the dll to see which number you receive and what you are returning ?

    Cédric Moonen Software developer
    Charting control [v1.3]

    P Offline
    P Offline
    parth p
    wrote on last edited by
    #3

    I am not actually sure how to debug DLL :s

    - Stop thinking in terms of limitations and start thinking in terms of possibilities -

    C C 2 Replies Last reply
    0
    • P parth p

      Hi, I have a DLL written in C and I am calling it from VB and I am using Visual Studio 6. The problem I have is that I'm trying to receive RANDOM number from C and add to my VB form. But the number received is not random or is way to big than what I pass into the parameter. Here's the Code for .CPP #include "stdafx.h" #include #include #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } int _stdcall rndNum(int iMaxNum) { int iNum; //srand( (unsigned)iMaxNum ); srand( (unsigned)time( NULL ) ); iNum = rand() % iMaxNum; return iNum; } .DEF file LIBRARY CdllRndNum EXPORTS rndNum @1 VB Code Private Declare Function rndNum Lib "F:\Year - 2\CdllRndNum\Debug\CdllRndNum.dll" (ByRef iMaxNum As Integer) As Integer Sub AddNums() Dim i As Integer For i = 0 To 40 List1.AddItem rndNum(100) Next i End Sub Result VB:

      28156
      28156
      28156
      28156
      28156 and same and same again....

      Result C:

      Number is 30
      Number is 43
      Number is 16
      Number is 24
      Number is 25
      Number is 63
      Number is 62
      Number is 84
      Number is 40
      Number is 64
      Number is 77
      Number is 79
      Press any key to continue

      The C code gives me perfect results in C Console app. But not in VB. Anyone any idea what is going wrong??:confused::confused::confused:

      - Stop thinking in terms of limitations and start thinking in terms of possibilities -

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #4

      killerp wrote:

      ...I'm trying to receive RANDOM number from C and add to my VB form.

      Unless this is just a test to see if the two can communicate, or you are just wanting to see how a DLL works, why not just let your VB app generate its own random numbers?

      killerp wrote:

      But the number received is not random...

      How are you verifying this?

      killerp wrote:

      But the number received is...way to big than what I pass into the parameter.

      This statement makes little sense. The argument passed to rndNum() is simply used to seed the random number generator. It has no bearing on anything else.

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      P 2 Replies Last reply
      0
      • P parth p

        I am not actually sure how to debug DLL :s

        - Stop thinking in terms of limitations and start thinking in terms of possibilities -

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #5

        here's how i debug DLL + VB situations: 1. in the C++ project, "run" the DLL. 2. VS will prompt you for a debug target. tell it to run VB.exe (VB itself) 3. when VB opens, open your VB application 4. put a breakpoint on the call to your DLL 5. run your VB app now you can step into your C++ DLL

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • P parth p

          I am not actually sure how to debug DLL :s

          - Stop thinking in terms of limitations and start thinking in terms of possibilities -

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #6

          Quoted from this page[^]: One important note is that all parameters must be passed with the ByVal keyword. This is because Visual Basic always passes parameters ByRef by default. Since C/C++ usually passes by value, this must be specified in the Visual Basic declaration. This is all that is necessary when standard C variable types (int, long, etc.) are used, however complex data types like strings and arrays require more overhead on both the client and DLL sides. They also specify that a VB integer correspond to a short in C/C++. So, change the C function to accept a short and check if it works (and don't forget the ByVal).

          Cédric Moonen Software developer
          Charting control [v1.3]

          P 1 Reply Last reply
          0
          • P parth p

            Hi, I have a DLL written in C and I am calling it from VB and I am using Visual Studio 6. The problem I have is that I'm trying to receive RANDOM number from C and add to my VB form. But the number received is not random or is way to big than what I pass into the parameter. Here's the Code for .CPP #include "stdafx.h" #include #include #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } int _stdcall rndNum(int iMaxNum) { int iNum; //srand( (unsigned)iMaxNum ); srand( (unsigned)time( NULL ) ); iNum = rand() % iMaxNum; return iNum; } .DEF file LIBRARY CdllRndNum EXPORTS rndNum @1 VB Code Private Declare Function rndNum Lib "F:\Year - 2\CdllRndNum\Debug\CdllRndNum.dll" (ByRef iMaxNum As Integer) As Integer Sub AddNums() Dim i As Integer For i = 0 To 40 List1.AddItem rndNum(100) Next i End Sub Result VB:

            28156
            28156
            28156
            28156
            28156 and same and same again....

            Result C:

            Number is 30
            Number is 43
            Number is 16
            Number is 24
            Number is 25
            Number is 63
            Number is 62
            Number is 84
            Number is 40
            Number is 64
            Number is 77
            Number is 79
            Press any key to continue

            The C code gives me perfect results in C Console app. But not in VB. Anyone any idea what is going wrong??:confused::confused::confused:

            - Stop thinking in terms of limitations and start thinking in terms of possibilities -

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #7

            also, try using "Long" instead of "Integer" for the VB declaration. an Integer is 16 bits, a Long is 32 bits (like a C int)

            image processing toolkits | batch image processing

            P 1 Reply Last reply
            0
            • D David Crow

              killerp wrote:

              ...I'm trying to receive RANDOM number from C and add to my VB form.

              Unless this is just a test to see if the two can communicate, or you are just wanting to see how a DLL works, why not just let your VB app generate its own random numbers?

              killerp wrote:

              But the number received is not random...

              How are you verifying this?

              killerp wrote:

              But the number received is...way to big than what I pass into the parameter.

              This statement makes little sense. The argument passed to rndNum() is simply used to seed the random number generator. It has no bearing on anything else.

              "Love people and use things, not love things and use people." - Unknown

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              P Offline
              P Offline
              parth p
              wrote on last edited by
              #8

              I'm doing bit of research on random numbers from different language, and I am already using VB, C# and C So I see what you are trying to say, but I want to use this.

              - Stop thinking in terms of limitations and start thinking in terms of possibilities -

              1 Reply Last reply
              0
              • C Cedric Moonen

                I am far from being a VB expert, but shouldn't you pass the integer ByVal instead of ByRef ? Did you try to debug the dll to see which number you receive and what you are returning ?

                Cédric Moonen Software developer
                Charting control [v1.3]

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #9

                Cedric Moonen wrote:

                ...but shouldn't you pass the integer ByVal instead of ByRef ?

                I don't see this making any difference since the ByVal/ByRef keyword only affect how the argument is sent to rndNum(), not what type of value is returned from it.

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                C 1 Reply Last reply
                0
                • D David Crow

                  Cedric Moonen wrote:

                  ...but shouldn't you pass the integer ByVal instead of ByRef ?

                  I don't see this making any difference since the ByVal/ByRef keyword only affect how the argument is sent to rndNum(), not what type of value is returned from it.

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #10

                  I don't know how it is implemented in VB, but if the ByRef is in fact passing the address of the variable, then this is the problem because that will have an impact on what the function returns (because the random number will be between 0 and that number).

                  Cédric Moonen Software developer
                  Charting control [v1.3]

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Quoted from this page[^]: One important note is that all parameters must be passed with the ByVal keyword. This is because Visual Basic always passes parameters ByRef by default. Since C/C++ usually passes by value, this must be specified in the Visual Basic declaration. This is all that is necessary when standard C variable types (int, long, etc.) are used, however complex data types like strings and arrays require more overhead on both the client and DLL sides. They also specify that a VB integer correspond to a short in C/C++. So, change the C function to accept a short and check if it works (and don't forget the ByVal).

                    Cédric Moonen Software developer
                    Charting control [v1.3]

                    P Offline
                    P Offline
                    parth p
                    wrote on last edited by
                    #11

                    Hi first of all thanks for quick reply. :) Some how I realised that the speed of DLL and EXE could vary, so now I have timer which will get number every second. AND I did what you just said and now it's giving me random number between 0(1) - 100 (never seen 0 or 100) but yeh. But what happens now is that the numbers comes in ascending order.

                    2
                    12
                    43
                    56
                    78
                    90
                    95
                    1
                    3
                    5
                    and so on...

                    What surprises me most is that the same code works perfect with C but not with VB. :confused:

                    - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                    1 Reply Last reply
                    0
                    • C Chris Losinger

                      also, try using "Long" instead of "Integer" for the VB declaration. an Integer is 16 bits, a Long is 32 bits (like a C int)

                      image processing toolkits | batch image processing

                      P Offline
                      P Offline
                      parth p
                      wrote on last edited by
                      #12

                      Hi I did it but the results don't change much... cuz the MAX number I am thinking to use is 3000 so...

                      - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                      1 Reply Last reply
                      0
                      • D David Crow

                        killerp wrote:

                        ...I'm trying to receive RANDOM number from C and add to my VB form.

                        Unless this is just a test to see if the two can communicate, or you are just wanting to see how a DLL works, why not just let your VB app generate its own random numbers?

                        killerp wrote:

                        But the number received is not random...

                        How are you verifying this?

                        killerp wrote:

                        But the number received is...way to big than what I pass into the parameter.

                        This statement makes little sense. The argument passed to rndNum() is simply used to seed the random number generator. It has no bearing on anything else.

                        "Love people and use things, not love things and use people." - Unknown

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        P Offline
                        P Offline
                        parth p
                        wrote on last edited by
                        #13

                        Hi, Thanks for reply. I haven't got any special method to verify the numbers received... It's just common sense to recognise Random numbers. The other thing I figured out just now is that the problem lies in srand( (unsigned)time( NULL ) ); I just removed this line and everything is working as I expected! But now i will get same numbers every time I use it. This simple dll is becoming more and more complicated...

                        - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                        C D 2 Replies Last reply
                        0
                        • P parth p

                          Hi, Thanks for reply. I haven't got any special method to verify the numbers received... It's just common sense to recognise Random numbers. The other thing I figured out just now is that the problem lies in srand( (unsigned)time( NULL ) ); I just removed this line and everything is working as I expected! But now i will get same numbers every time I use it. This simple dll is becoming more and more complicated...

                          - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                          C Offline
                          C Offline
                          Cedric Moonen
                          wrote on last edited by
                          #14

                          You should call srand only once, for example in the DllMain.

                          Cédric Moonen Software developer
                          Charting control [v1.3]

                          P CPalliniC 2 Replies Last reply
                          0
                          • P parth p

                            Hi, I have a DLL written in C and I am calling it from VB and I am using Visual Studio 6. The problem I have is that I'm trying to receive RANDOM number from C and add to my VB form. But the number received is not random or is way to big than what I pass into the parameter. Here's the Code for .CPP #include "stdafx.h" #include #include #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } int _stdcall rndNum(int iMaxNum) { int iNum; //srand( (unsigned)iMaxNum ); srand( (unsigned)time( NULL ) ); iNum = rand() % iMaxNum; return iNum; } .DEF file LIBRARY CdllRndNum EXPORTS rndNum @1 VB Code Private Declare Function rndNum Lib "F:\Year - 2\CdllRndNum\Debug\CdllRndNum.dll" (ByRef iMaxNum As Integer) As Integer Sub AddNums() Dim i As Integer For i = 0 To 40 List1.AddItem rndNum(100) Next i End Sub Result VB:

                            28156
                            28156
                            28156
                            28156
                            28156 and same and same again....

                            Result C:

                            Number is 30
                            Number is 43
                            Number is 16
                            Number is 24
                            Number is 25
                            Number is 63
                            Number is 62
                            Number is 84
                            Number is 40
                            Number is 64
                            Number is 77
                            Number is 79
                            Press any key to continue

                            The C code gives me perfect results in C Console app. But not in VB. Anyone any idea what is going wrong??:confused::confused::confused:

                            - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #15

                            1. You need to use the following declaration

                            Private Declare Function rndNum Lib "F:\Year - 2\CdllRndNum\Debug\CdllRndNum.dll" (ByVal iMaxNum As Long) As Long

                            killerp wrote:

                            srand( (unsigned)time( NULL ) );

                            2. The above is a mistake. Don't initialise the random number generator each time you're calling it (and remember, time returns seconds). :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                            In testa che avete, signor di Ceprano?

                            1 Reply Last reply
                            0
                            • C Cedric Moonen

                              You should call srand only once, for example in the DllMain.

                              Cédric Moonen Software developer
                              Charting control [v1.3]

                              P Offline
                              P Offline
                              parth p
                              wrote on last edited by
                              #16

                              Yeh, I got this sorted now. It was srand. Because the speed of accessing values and passing value is so fast it was passing same time into srand. SO now i got static int and it will make sure that srand only takes place once when the application is started. Anyway thanks for all you guys support. THANK YOU ALL.

                              - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                              1 Reply Last reply
                              0
                              • C Cedric Moonen

                                You should call srand only once, for example in the DllMain.

                                Cédric Moonen Software developer
                                Charting control [v1.3]

                                CPalliniC Offline
                                CPalliniC Offline
                                CPallini
                                wrote on last edited by
                                #17

                                Probably will be better exporting srand too, otherwise he needs to restart the executable each time he needs a different sequence. :)

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                                In testa che avete, signor di Ceprano?

                                1 Reply Last reply
                                0
                                • P parth p

                                  Hi, Thanks for reply. I haven't got any special method to verify the numbers received... It's just common sense to recognise Random numbers. The other thing I figured out just now is that the problem lies in srand( (unsigned)time( NULL ) ); I just removed this line and everything is working as I expected! But now i will get same numbers every time I use it. This simple dll is becoming more and more complicated...

                                  - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #18

                                  In order for what you have to work, you're going to need a really, really slow computer. The problem is not int vs. long vs. short, or ByVal vs ByRef, but rather that rndNum() is being called so fast, that each call to srand() is seeding with the same value. To demonstrate this, put a call to Sleep(750) between srand() and rand(). Moving srand() to DllMain() is what you need.

                                  "Love people and use things, not love things and use people." - Unknown

                                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                  1 Reply Last reply
                                  0
                                  • P parth p

                                    Hi, I have a DLL written in C and I am calling it from VB and I am using Visual Studio 6. The problem I have is that I'm trying to receive RANDOM number from C and add to my VB form. But the number received is not random or is way to big than what I pass into the parameter. Here's the Code for .CPP #include "stdafx.h" #include #include #include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } int _stdcall rndNum(int iMaxNum) { int iNum; //srand( (unsigned)iMaxNum ); srand( (unsigned)time( NULL ) ); iNum = rand() % iMaxNum; return iNum; } .DEF file LIBRARY CdllRndNum EXPORTS rndNum @1 VB Code Private Declare Function rndNum Lib "F:\Year - 2\CdllRndNum\Debug\CdllRndNum.dll" (ByRef iMaxNum As Integer) As Integer Sub AddNums() Dim i As Integer For i = 0 To 40 List1.AddItem rndNum(100) Next i End Sub Result VB:

                                    28156
                                    28156
                                    28156
                                    28156
                                    28156 and same and same again....

                                    Result C:

                                    Number is 30
                                    Number is 43
                                    Number is 16
                                    Number is 24
                                    Number is 25
                                    Number is 63
                                    Number is 62
                                    Number is 84
                                    Number is 40
                                    Number is 64
                                    Number is 77
                                    Number is 79
                                    Press any key to continue

                                    The C code gives me perfect results in C Console app. But not in VB. Anyone any idea what is going wrong??:confused::confused::confused:

                                    - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                                    P Offline
                                    P Offline
                                    pabloraul
                                    wrote on last edited by
                                    #19

                                    i may not be an expert but if all you need is a random number in VB you should use: ' Initialize the random-number generator. Randomize() ' Generate random value between 1 and 6. Dim value As Integer = CInt(Int((6 * Rnd()) + 1))

                                    P 1 Reply Last reply
                                    0
                                    • P pabloraul

                                      i may not be an expert but if all you need is a random number in VB you should use: ' Initialize the random-number generator. Randomize() ' Generate random value between 1 and 6. Dim value As Integer = CInt(Int((6 * Rnd()) + 1))

                                      P Offline
                                      P Offline
                                      parth p
                                      wrote on last edited by
                                      #20

                                      Hey Thanks for Reply, but I have already mentioned that I DO NOT WANT TO USE VB (and yes I do know how to do it in VB. For .NET there is another method available by using Random class and then Random.Next()) But anyway thanks for reply. :)

                                      - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups