Page 2 of 3 FirstFirst 123 LastLast
Results 41 to 80 of 91

Thread: Nibbles, bit operations and C dll's [RESOLVED]

  1. #41
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    I have to make sure what exactly the error was but at the moment I'm sitting in front of a computer where VC++ is not installed. think it was something like "It was not possible to add a reference to this library" when I tried to add the reference in the VB project/references menu.
    But you're not making an ActiveX DLL so you will not be able to set a reference to it. What you're making is a standard DLL file so you will need to use a Declare statement just as with API calls.

  2. #42

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    But you're not making an ActiveX DLL so you will not be able to set a reference to it. What you're making is a standard DLL file so you will need to use a Declare statement just as with API calls.
    Of course, thank you. Very obviously I'm quite new to this jungle. I just can't wait till I go back home and try it!

    How about the def file thing? Do you know if I can add it as a cpp source code file although it's a text file?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  3. #43

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    By the way, is it mandatory to place the dll in windows/system? Could I have it in the app directory or elsewhere?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #44
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    How about the def file thing? Do you know if I can add it as a cpp source code file although it's a text file?
    I don't understand. Did you add the file by clicking Project > Add To Project > Files or did you use File > New? You should use the first option, you can then select to add a definition file by selecting the correct item in the "Files of type" drop down of the Open dialog box.

    It doesn't matter where you put the DLL file, however if it's not in the System folder or in the application folder you must specify the path of it in the Declare statement in VB.

  5. #45

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    Did you add the file by clicking Project > Add To Project > Files or did you use File > New? You should use the first option, you can then select to add a definition file by selecting the correct item in the "Files of type" drop down of the Open dialog box.
    I don't remember how I did it but from what you say I probably used the second option. I'll check it out this evening.
    Thanks for being so patient with my clumsy attempts
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #46
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles!

    You don't need to add a reference, just declare it as I showed you above

    Oh... I see this question has been answered.
    Last edited by moeur; Apr 18th, 2005 at 10:01 AM.

  7. #47

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    I may be going too fast as I haven't had a chance yet to check whether "the magic" works, but I've just thought of another question: can I pass a control to a procedure in a c++ dll? The idea is I have a program that plots on a picturebox according to a complicated algorithm and the result is the plot takes about 10 s. to complete. maybe I could speed that up if I could call a C sub from VB.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #48
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Pass the hDC of the PictureBox instead.

  9. #49
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    I may be going too fast as I haven't had a chance yet to check whether "the magic" works, but I've just thought of another question: can I pass a control to a procedure in a c++ dll? The idea is I have a program that plots on a picturebox according to a complicated algorithm and the result is the plot takes about 10 s. to complete. maybe I could speed that up if I could call a C sub from VB.
    Probably, but you would hve to know MFC. I think you can pass an HANDLE around which is a window handle.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  10. #50

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    Pass the hDC of the PictureBox instead.
    OK, but then how about plotting? I mean, the line method, scale and all that stuff.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  11. #51
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    OK, but then how about plotting? I mean, the line method, scale and all that stuff.
    They don't exist in C/C++ anyway, you would need to use the GDI APIs to draw on the picbox. Like MoveToEx() to move to a particular point and then use LineTo() to draw a line.

  12. #52

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by Joacim Andersson
    They don't exist in C/C++ anyway, you would need to use the GDI APIs to draw on the picbox. Like MoveToEx() to move to a particular point and then use LineTo() to draw a line.
    That's more or less what I expected. But I think it's enough for now and maybe I should keep from thinking big before I can handle the simplest things.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  13. #53
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    That's more or less what I expected. But I think it's enough for now and maybe I should keep from thinking big before I can handle the simplest things.
    While you should not always implement for performance, you should plan for performance. Eventually you may really need to plot with a faster platform than VB. However there seems to be a performance boundary line, allowing you to break the performance responsibilities into 2 functions without sacrificing overall performance:

    1) Create a C-DLL for string parsing, and do your plotting in VB
    2) If the plotting is painfuilly slow, keep the C-DLL for string parsing and then work on an MFC-DLL for plotting.

    In the end you can keep both functions seperate without trying to mix them both into the same DLL.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  14. #54
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles! [Happy with your answers]

    I don't think you would gain very much by moving the graphic plotting to a C DLL. The built in VB graphic functions are extremly slow but you can use all GDI functions from VB instead. They won't run much faster in a C application compared to a VB application, all you add is one more API call (the one to your own DLL).

  15. #55

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by moeur
    If you need help translating the magic I can help too.
    Well, I'm stuck so after all I'm afraid I do badly need help.

    It appears that my code is faulty for it returns all values of Val12bit
    as well as that of Max12bit as 0. This is what I've done:

    In VB:

    Private Declare Sub ProcessData Lib "Mydll.dll" (DataByte As Byte, _
    Val12bit As Long, _
    ByVal Max12bit As Long, _
    ByVal BufferSize As Long)

    The C code according to your template:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    __declspec( dllexport ) void _stdcall ProcessData (char DataByte[][3],
    short Val12bit[], long Max12bit,long BufferSize)
    
    {
    
    	/* WELL, I'VE JUST REALIZED i and j should be long.
    	Could that alone explain it? (I can't test it at the moment) */
    	int i;
    	int j;
    	
    	Max12bit=0;
    	for (i=0;i<=BufferSize;i++)
    		j=2*i;
    		Val12bit[j] = (DataByte[i][2] & 240) * 16 | DataByte[i][0];
                    Val12bit[j + 1] = (DataByte[i][2] & 15) * 256 | DataByte[i][1];
            if (Max12bit < Val12bit[j])
    			Max12bit = Val12bit[j];
            if (Max12bit < Val12bit[j + 1])
    			Max12bit = Val12bit[j + 1];		
    	return;
    }
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  16. #56

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles!

    Quote Originally Posted by krtxmrtz
    Well, I'm stuck so after all I'm afraid I do badly need help.

    Code:
    	/* WELL, I'VE JUST REALIZED i and j should be long.
    	Could that alone explain it? (I can't test it at the moment) */
    	int i;
    	int j;
    I've rebuilt the dll after changing these to long and... oh no, it wasn't that, integer i and j didn't account for the returned 0 values. Help!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  17. #57
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    First of all there is an error in your declaration, should be
    Code:
    __declspec( dllexport ) void _stdcall ProcessData (char DataByte[][3],
    short Val12bit[], long* Max12bit, long BufferSize)
    
    {
    
    	int i;
    	int j;
    	
    	*Max12bit=0;
    
    	for (i=0;i<=BufferSize;i++)
    		j=2*i;
    		Val12bit[j] = (DataByte[i][2] & 240) * 16 | DataByte[i][0];
                    Val12bit[j + 1] = (DataByte[i][2] & 15) * 256 | DataByte[i][1];
            if (*Max12bit < Val12bit[j])
    			*Max12bit = Val12bit[j];
            if (*Max12bit < Val12bit[j + 1])
    			*Max12bit = Val12bit[j + 1];		
    	return;
    }
    Show me how you're dimensioning your arrays and calling the routine from VB.
    Should be
    VB Code:
    1. Private Declare Sub ProcessData Lib "MyDll.dll" ( _
    2.   DataByte As Byte, _
    3.   Val12bit As Integer, _
    4.   Max12bit As Long, _
    5.   ByVal BufferSize As Long _
    6. )
    and something like
    VB Code:
    1. Dim DataByte(0 To 3, 0 To 100) As Byte
    2. Dim Val12bit(0 To 100) As Integer
    3. Dim Max12bit As Long
    4. Dim BufferSize As Long
    5. BufferSize = 100
    And call it like
    VB Code:
    1. Call ProcessData(DataByte(0, 0), Val12bit(0), Max12bit, BufferSize)
    Last edited by moeur; Apr 19th, 2005 at 11:52 AM.

  18. #58

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles: still need help

    Quote Originally Posted by moeur
    Show me how you're dimensioning your arrays and calling the routine from VB.
    VB Code:
    1. 'These are my declarations:
    2.  
    3. Private Declare Sub ProcessData Lib "Mydll.dll" (DataByte As Byte, _
    4. Val12bit As Integer, _
    5. Max12bit As Integer, _
    6. ByVal BufferSize As Long)
    7.  
    8. 'Data declarations:
    9.  
    10. Dim Max12bit As Integer
    11. Dim DiskData() As Byte
    12. Dim Val12bit() As Integer
    13.  
    14. 'Calling of dll code:
    15.  
    16.     Dim BufferSize As Long
    17.     'dsize is the size of the data area in the file
    18.     'i.e. dsize = filesize - headersize
    19.  
    20.     BufferSize = 2 * dSize \ 3
    21.  
    22.     ReDim Val12bit(BufferSize )
    23.  
    24.     Call ProcessData(DiskData(0, 0), Val12bit(0), Max12bit, BufferSize)
    This however produces an error (see figure):
    Attached Images Attached Images   
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  19. #59
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    Shouldn't it be?
    VB Code:
    1. ReDim Val12bit(2*BufferSize )
    Since in your C code,
    i goes from 0 to Buffersize-1 and j=2*i

  20. #60
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles: still need help

    Besides the fact pointed out by moeur above there is also a logical error in your C code. The for loop should use the curly brackets...
    Code:
    for (i=0; i<BufferSize; i++) {
        j=2*i;
        //... the rest of the code
    }
    return;

  21. #61

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles: still need help

    Quote Originally Posted by Joacim Andersson
    Besides the fact pointed out by moeur above there is also a logical error in your C code. The for loop should use the curly brackets...
    Code:
    for (i=0; i<BufferSize; i++) {
        j=2*i;
        //... the rest of the code
    }
    return;
    You're right, the brackets were missing... it's been so long since I was fluent in C...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  22. #62

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles: still need help

    Quote Originally Posted by moeur
    Shouldn't it be?
    VB Code:
    1. ReDim Val12bit(2*BufferSize )
    Since in your C code,
    i goes from 0 to Buffersize-1 and j=2*i
    Actually it was the other way round:

    BufferSize = dsize\3 and not 2*dsize\3

    I don't get any errors now, but the Val12bit values calculated in the dll are wrong because some of them come out negative. Maybe the variable type is not correct and it should be "unsigned" or something...?

    But we're getting close.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  23. #63
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles: still need help

    I don't remember the order, but isn't a bit wise operation evaluated before a multiplication in C? If so then this code is wrong:
    Code:
    Val12bit[j] = (DataByte[i][2] & 240) * 16 | DataByte[i][0];
    Val12bit[j + 1] = (DataByte[i][2] & 15) * 256 | DataByte[i][1];
    To me it looks like you, on the first line, multiply with 16 | DataByte[i][0] which is not what you want to do. Try this:
    Code:
    Val12bit[j] = ((DataByte[i][2] & 240) * 16) | DataByte[i][0];
    Val12bit[j + 1] = ((DataByte[i][2] & 15) * 256) | DataByte[i][1];

  24. #64
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles: still need help

    Quote Originally Posted by Joacim Andersson
    Besides the fact pointed out by moeur above there is also a logical error in your C code. The for loop should use the curly brackets...
    Code:
    for (i=0; i<BufferSize; i++) {
        j=2*i;
        //... the rest of the code
    }
    return;
    This can be further optimized by removing the painfully slow multiplication operation and replacing it with a bit shift:
    Code:
    j = i << 1;
    Always replace multiplications and divisions of (2) with bit shifts instead.
    Last edited by Dave Sell; Apr 20th, 2005 at 08:55 AM. Reason: I can't remember the bit-shift syntax exactly!
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  25. #65
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    I don't get any errors now, but the Val12bit values calculated in the dll are wrong because some of them come out negative. Maybe the variable type is not correct and it should be "unsigned" or something...?
    Change Val12bit to type Long in both VB and C program.
    Actually it was the other way round:
    BufferSize = dsize\3 and not 2*dsize\3
    Did you make some change to the C++ code to compensate? If not you are still doing something wrong here.

  26. #66
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    Quote Originally Posted by Dave Sell
    Always replace multiplications and divisions of (2) with bit shifts instead.
    This isn't necessary in C++ since the compiler will do it for you. Now assembler is a different story.

  27. #67
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Nibbles: still need help

    Quote Originally Posted by moeur
    Change Val12bit to type Long in both VB and C program.
    I don't think that's the problem even if it's a good idea to use 32 bit integers instead since they are faster. I think the problem with the overflow error is the bitwise OR as I mentioned in my last post.

  28. #68
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles: still need help

    Quote Originally Posted by moeur
    This isn't necessary in C++ since the compiler will do it for you. Now assembler is a different story.
    Really? Are you sure? That would be sweet if they did.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  29. #69
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    The compilers know every trick in the book. That is why you can often produce faster code with C than with assembler.

  30. #70
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Nibbles!

    Quote Originally Posted by moeur
    In many cases a C compiler will produce faster code than you will produce writing in assembly language yourself. This is beacuse the compiler knows a lot of tricks that you don't. I've actually tested it.
    Can you post some code form both for me to test? Something no doubt memory heavy?

    I've written a few things in C/C++ that are about 100-200ms quicker in ASM (32-bit: both).

    Please, post something I can test..

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  31. #71
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    Can you post some code form both for me to test? Something no doubt memory heavy?

    I've written a few things in C/C++ that are about 100-200ms quicker in ASM (32-bit: both).
    I don't have any asm code anymore.
    Make sure that when you compile the C code, you are otimizing for maximum speed and that you are not compiling the Debug code.

    Of course if you know all the tricks too (like memory alignment issues) then you can produce asm code that is faster than C code since the C compiler is not 100% optimized, but I only know the obvious ones like Dave mentioned and hence produce slower code. That's why I don't use ASM anymore.

  32. #72
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles!

    Quote Originally Posted by «°°phReAk°°»
    Remember that a Megabyte, a Meg, an MB are 1048576 bytes. Most of the time though, manufacturers give specifications showing a Mebabyte is 1000000 bytes in size to let their products look 4.8576% more impressive.
    Actually, this is an incorrect statement. A Megabyte is not 1048576 bytes.

    See http://helios.augustana.edu/~dr/kibibytes.html.

    A Megabyte is 10^6, or 1,000,000 (a million) bytes.
    A Mebibyte is 2^20, or 1,048,576 bytes.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  33. #73

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles: still need help

    Quote Originally Posted by Joacim Andersson
    I don't remember the order, but isn't a bit wise operation evaluated before a multiplication in C? If so then this code is wrong:
    Code:
    Val12bit[j] = (DataByte[i][2] & 240) * 16 | DataByte[i][0];
    Val12bit[j + 1] = (DataByte[i][2] & 15) * 256 | DataByte[i][1];
    To me it looks like you, on the first line, multiply with 16 | DataByte[i][0] which is not what you want to do. Try this:
    Code:
    Val12bit[j] = ((DataByte[i][2] & 240) * 16) | DataByte[i][0];
    Val12bit[j + 1] = ((DataByte[i][2] & 15) * 256) | DataByte[i][1];
    It isn't that, I'm still getting negatives.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  34. #74
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Nibbles: still need help

    Quote Originally Posted by krtxmrtz
    It isn't that, I'm still getting negatives.
    I haven't really been following this thread much - as I do not know C...

    But a negative value is simply one with the high-order bit set and touching that high order bit in multiplication will usually cause an overflow error...

    Don't know if that means anything helpful!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  35. #75
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Nibbles: still need help

    Quote Originally Posted by szlamany
    I haven't really been following this thread much - as I do not know C...

    But a negative value is simply one with the high-order bit set and touching that high order bit in multiplication will usually cause an overflow error...

    Don't know if that means anything helpful!
    Ya I was just getting ready to suggest that he cast his result to a signed int...
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  36. #76

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles: still need help

    Quote Originally Posted by moeur
    Change Val12bit to type Long in both VB and C program.
    Did you make some change to the C++ code to compensate? If not you are still doing something wrong here.
    Still not working, the dll returns some negative Val12bit values. Could it have something to do with declaring the variables signed or unsigned?
    This is what it looks like now:
    Code:
    'The VB side
    Private Declare Sub ProcessData Lib "Mydll.dll" (DataByte As Byte, _
    Val12bit As Long, _
    Max12bit As Long, _
    ByVal BufferSize As Long)
    
    Dim Max12bit as Long
    Dim DiskData() As Byte
    Dim Val12bit() As Long
    
    '...
    '...
    '...
    'Later on, after the data file has been read in:
    
        ReDim DiskData(2, dSize \ 3 - 1)
    
    'In the data processing sub:
    
        Dim BufferSize As Long
        BufferSize = dSize \ 3
        ReDim Val12bit(2*BufferSize)
        Call ProcessData(DiskData(0, 0), Val12bit(0), Max12bit, BufferSize)
    '...
    '...
    
    '**********************************************************************
    'The C part
    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    __declspec( dllexport ) void _stdcall ProcessData (char DataByte[][3],
    long Val12bit[], long* Max12bit, long BufferSize)
    
    {
    
    	long i;
    	long j;
    	
    	*Max12bit=0;
    
    	for (i=0;i<BufferSize;i++)
    	{
    		j=i<<1; /* Multiply by 2 by doing a left shift */
    		Val12bit[j] = ((DataByte[i][2] & 240) * 16) | DataByte[i][0];
            Val12bit[j + 1] = ((DataByte[i][2] & 15) * 256) | DataByte[i][1];
            if (*Max12bit < Val12bit[j])
    			*Max12bit = Val12bit[j];
            if (*Max12bit < Val12bit[j + 1])
    			*Max12bit = Val12bit[j + 1];	
    	}		
    	return;
    }
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  37. #77
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    Quote Originally Posted by szlamany
    But a negative value is simply one with the high-order bit set
    This is why I told him to declare Val12bit to Long about 10 posts back

  38. #78
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Nibbles: still need help

    Quote Originally Posted by moeur
    This is why I told him to declare Val12bit to Long about 10 posts back
    Just because it displays as negative is not a bad thing though - right? That's just a silly "display property" of signed integers with high-order bit set...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  39. #79

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Nibbles: still need help

    Quote Originally Posted by Dave Sell
    Ya I was just getting ready to suggest that he cast his result to a signed int...
    You mean declaring Val12bit as signed int in the dll? And how about signed long? Does such a type exist?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  40. #80
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Nibbles: still need help

    OK, need more info.
    Are the values that are not negative the correct values?
    Are the negative values only on the even or odd Val12bit[]s?
    Can you supply some data for us to work with?

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width