Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
almostkorean
Jul 9, 2001
eeeeeeeee
haha, wow. Thanks for the help everyone. I got it working now.

Adbot
ADBOT LOVES YOU

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
I guess this thread is the closest to what I'm looking for, not exactly C/C++ but rather the Win32 API.

Anyway, by any chance is there a function that returns the handle to the window it's being called from? (which be your own, except I'd be calling it from an injected DLL, I'm going to be drawing with GDI over what I'm injected into.) Something like FindWindow sounds a little rear end backwards when I'm already injected.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

slovach posted:

I guess this thread is the closest to what I'm looking for, not exactly C/C++ but rather the Win32 API.

Anyway, by any chance is there a function that returns the handle to the window it's being called from? (which be your own, except I'd be calling it from an injected DLL, I'm going to be drawing with GDI over what I'm injected into.) Something like FindWindow sounds a little rear end backwards when I'm already injected.

I'm not positive that I properly understood your question- are you trying to get the HWND associated with the thread in which you're executing? Look into EnumThreadWindows()- it does exactly what the name might imply.

shitty bill
Feb 8, 2003

slovach posted:

I guess this thread is the closest to what I'm looking for, not exactly C/C++ but rather the Win32 API.

Anyway, by any chance is there a function that returns the handle to the window it's being called from? (which be your own, except I'd be calling it from an injected DLL, I'm going to be drawing with GDI over what I'm injected into.) Something like FindWindow sounds a little rear end backwards when I'm already injected.

Since you don't know who called you in C++ you're going to need GetCurrentThreadId() in addition to what Plastic Jesus said.

Kenfoldsfive
Jan 1, 2003

The un-bitey-ness of a chicken's head and the "I don't want to cook that"-ness of a dog's body
This is the second program I've written giving me this error, so I come to you folk for help. I'm trying to pass a 2-dimensional array (it's a 1024 x 1024 array of ints) from my driver program to a function. The function I'm passing to has a prototype of

code:
void populate(int path[]);
And the relevant creation and passing portions of my driver function are:

code:
int path[1024][1024];
spf.populate(path[][]);
When I try to compile that, visual studio throws back a Syntax Error ']' pointing to the spf.populate line. I thought I had to specify the second dimension of the array, so I tried passing it as path[][1024] and got the same error. The only way I could get it to work in the last program I had with this problem was writing out arrays altogether, which was a huge pain and something I'd rather not do again.

csammis
Aug 26, 2003

Mental Institution
For starters, populate() takes a 1-D array and you're passing it a 2-D array :confused:

Standish
May 21, 2001

csammis posted:

For starters, populate() takes a 1-D array and you're passing it a 2-D array :confused:
To continue, the function call should be "spf.populate(path);", no square brackets needed.

code:
void populate(int path[][1024]);
...
int path[1024][1024]; 
spf.populate(path);
(The reason you need to specify the second array dimension "[1024]" in the function prototype so that the compiler can do its pointer arithmetic magic properly and translate the array index "path[i][j]" to the memory address "path + (i * 1024 * sizeof(int)) + (j * sizeof(int) ".)

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
Like slovach, I have a Windows API question and don't know which thread is best. So I'm posting here.

Did something happen to context menu creation in Vista? The following works just fine in every version of Windows except Vista (and actually works in Vista if you're using the Windows Classic theme):

code:

HMENU menu;
DWORD cmd;
POINT p;
HWND wnd;

wnd = my_global_hidden_window;
menu = CreatePopupMenu();
AppendMenu(menu, MF_STRING, MYMENU_CMD1, TEXT("menu item 1"));
AppendMenu(menu, MF_STRING, MYMENU_CMD2, TEXT("menu item 2"));
AppendMenu(menu, MF_STRING, MYMENU_CMD3, TEXT("menu item 3"));
AppendMenu(menu, MF_STRING, MYMENU_CMD4, TEXT("menu item 4"));
GetCursorPos(&p);
SetForegroundWindow(wnd);
cmd = TrackPopupMenuEx(menu, (TPM_RIGHTALIGN|TPM_RETURNCMD), p.x, p.y, wnd, NULL);

It will create a menu that's 4 items long, but there will be no text in any menu entry. The TrackPopupMenuEx() call works fine and will return the proper value for each entry. It's just the printing of text for the menu items that's failing and, like I said, only in the Vista default theme. I've tried explicitly using both Unicode and ASCII strings and that doesn't seem to matter.


Edit:

Figured it out, and it had nothing to do with the actual menu creation. I didn't have 'default: return(DefWindowProc())' in my windows WindowProc. Sure am glad that it took me 4 days to figure out that I'm a retard.

Plastic Jesus fucked around with this message at 15:30 on Apr 18, 2008

Kenfoldsfive
Jan 1, 2003

The un-bitey-ness of a chicken's head and the "I don't want to cook that"-ness of a dog's body

csammis posted:

For starters, populate() takes a 1-D array and you're passing it a 2-D array :confused:

Well that's what I get for staying up all night to finish this :doh:

Standish posted:

:science: arrays

Thanks! You guys are the coolest! :glomp:

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
I'm moving this here from the General thread, because this is where it belongs.


clockwork automaton posted:

:D It's okay though, because I figured it out. It was an issue with user space memory and kernel space memory all I had to do was use strcpy and it was saved.

You've actually got quite a lot of bad going on with your code. I only glanced at the code up on pastebin, but I noticed these things:

* You're allocating memory for 'name' in main() and never freeing it. What's worse is that you never actually use this memory since you later set name to point to something in argv[].

* You don't check the return value of any of your memory allocation routines, a tremendously bad idea.

* In your vector_resize() function you will end up with members of your array pointing to bad memory since you do this:

code:
   for(i=0; i<vector_len; i++)
   {
      vector_new[i] = (struct data*)kmalloc(sizeof(struct data*), GFP_KERNEL);
      vector_new[i]->name = vector[i]->name;
      vector_new[i]->bytes = vector[i]->bytes;
   }
 
   vector_free();
   vector = vector_new;
Also, what's the point of your vector datatype in the first place?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Hey can I ask you guys to critique my code? I wrote a mastermind game using RPC and I have a feeling there is a memory leak in the server.

http://codepad.org/gqDb9m76
http://codepad.org/5KT3eVmy

These defines are in the mastermind.h file in case you are wondering.

#define SLOTS 4
#define COLORS 8
#define TRIES 10

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
On the client:

* You have a potential buffer overflow on line #66, since there won't be a null terminator (pass SLOTS as the length param to read_line). This could get messy on line 110 where you do an strlen() on the read buffer. You should also explicitly bzero() the input buffer.

* You don't check to see if received is a valid pointer before you free() it. If someone does "9999" on the first turn, that'll crash.


On the server:

* You don't check to make sure that your calls to calloc() on liness #57 and #90 don't fail.

* You don't check to ensure that input is non-NULL in the compare() function. You should also check to ensure that it is at least (SLOTS + 1) long.

* You don't allocate any space for 'correct' on line 69. In fact, I think that your declaration will result in a const char *, not a char *, so the assignment on line 74 should fail spectacularly. That whole loop is kinda weird. Wouldn't something like this work just as well and more simply?
code:

memset(output, 0, SLOTS);
for(i = 0; i < SLOTS; i++){
   if((input[i] - '0') == randomColors[i])
      output[i] = '1';
}
* There is a tiny memory leak on line 35. result already points to a valid buffer at that point, so you should free that before you do the assignment from getColors();

* I have no idea how RPC deals with freeing malloc()'d data, but hopefully it does since you never free anything.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Thanks for the tips Plastic Jesus. The reason that loop is so weird is because I'm supposed to give a 0 when they guess the right color but it's not in the correct position and a 1 when it's the right color and the right position.

Also I had never thought about calloc returning a null pointer. What is the best course of action in that case? I can't just exit the server because the client would be left hanging for who knows how long.

You were right about their being no calls to free in the server. Since result is a static variable I just check to make sure that it's not null and free it at the beginning each time. That should ensure that it's not leaking memory each call.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

MEAT TREAT posted:

Thanks for the tips Plastic Jesus. The reason that loop is so weird is because I'm supposed to give a 0 when they guess the right color but it's not in the correct position and a 1 when it's the right color and the right position.

Also I had never thought about calloc returning a null pointer. What is the best course of action in that case? I can't just exit the server because the client would be left hanging for who knows how long.

The best thing to do is have a message the server sends the client to tell it that it's being shut down, and preallocate all memory needed to send that message at the beginning of the program. Then if calloc ever returns null you can send that last message before shutting down without having to allocate more memory.

It's not that terrible to shut down and leave the client hanging, though, since the client should be able to deal with timeouts anyway (what happens if the network goes down?) - better than trying to forge ahead without memory.

Lexical Unit
Sep 16, 2003

Is this:
code:
ifstream in("filename");
size_t file_size in.rdbuf()->in_avail();
different than this:
code:
ifstream in("filename");
size_t file_size in.rdbuf()->pubseekoff(ios::beg, ios::end);
in.rdbuf()->pubseekpos(ios::beg);
?

Edit: I mean, does it always get the same answer and does it have the same set of side effects and gotchas?

Lexical Unit fucked around with this message at 21:52 on Apr 22, 2008

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Er, does that even compile?

Lexical Unit
Sep 16, 2003

Nope! Not without an equal sign after file_size in both examples :downs: See a running example here.

Lexical Unit fucked around with this message at 00:05 on Apr 23, 2008

Vanadium
Jan 8, 2005

No, current buffer contents have nothing to do with the size of the underlying file.

Lexical Unit
Sep 16, 2003

That's what I thought but I was just checking since it seems to work for everything I've thrown at it. There's also this site which says that this code
code:
int FileSize(const char* sFileName)
{
  std::ifstream f;
  f.open(sFileName, std::ios_base::binary | std::ios_base::in);
  if (!f.good() || f.eof() || !f.is_open()) { return 0; }
  f.seekg(0, std::ios_base::beg);
  std::ifstream::pos_type begin_pos = f.tellg();
  f.seekg(0, std::ios_base::end);
  return static_cast<int>(f.tellg() - begin_pos);
}
Might return a value for which the "size of the file may be larger than what is reported."

Leading me to wonder if the pubseekoff() method has the same limitation, and if that limitation is due to the same internal buffer that in_avail() uses, which in turn leads me to wonder if maybe in_avail() really does work just as well as either seek(end)/seek(beg) methods.

Adiabatic
Nov 18, 2007

What have you assholes done now?
Hi all, I'm currently taking an entry-level C++ programming class and can't for the life of me think of where to begin on this one. I'll post up the instructions if anyone would be so kind as to give me some pseudocode or any way of doing this with my current (See: very limited) knowledge, basically up to and a bit past arrays.

Write a program that will allow the user to rate various features of the 9 flavors of Dorito brand Dorito's. Here are the details of the program:

* The user should have the option to select any of the available 9 flavors
o Black Pepper Jack, Blazin' Buffalo Ranch, Cool Ranch, Fiery Habanero, Nacho Cheese, Ranchero, Spicy Nacho, Salsa Verde, or Taco
* The user will then rate on a scale of 1 to 5 the following items
o Overall taste, overall color, overall after-breath
* Your program will then accumulate all the entries and average up each rating as it relates to each flavor. For example, if 4 ratings were entered for Taco flavor, you should add up all the ratings for taste, color, after-breath, and find the average by dividing each total by the number of ratings (in this case 4).
* Allow your program to loop so that the rater can select different flavors and enter different values -- any number of times.
* When the input is complete, print out only the flavors that were rated along with the following information:
o The name of the flavor rated
o The number of raters
o The average rating for taste, color, and after-breath


This program has made me hate doritos.

haveblue
Aug 15, 2005



Toilet Rascal
Your problem right now seems to be coming up with a way to organize the data you're gathering. Do you know about structures yet?

TSDK
Nov 24, 2003

I got a wooden uploading this one

Adiabatic posted:

Hi all, I'm currently taking an entry-level C++ programming class and can't for the life of me think of where to begin on this one.

quote:

Students: don't ask or discuss how to get around your school's network security, and don't ask for help with your homework. If you really must ask the internet for help with an assignment, try the Science, Philosophy, and Education subforum.

Read your notes and ask your TA. Feel free to come back and ask specific language related problems when you get stuck, but don't expect to be spoon-fed.

Adiabatic
Nov 18, 2007

What have you assholes done now?

HB posted:

Your problem right now seems to be coming up with a way to organize the data you're gathering. Do you know about structures yet?

Nope. Right now the ability to store the information then recall it in an array is easy, it's the last part, printing only what was rated with the other info thats catching me up.

TSDK posted:

Read your notes and ask your TA. Feel free to come back and ask specific language related problems when you get stuck, but don't expect to be spoon-fed.

But but but... I'll give you car repair advice if you do :D

tef
May 30, 2004

-> some l-system crap ->

Adiabatic posted:

Nope. Right now the ability to store the information then recall it in an array is easy, it's the last part, printing only what was rated with the other info thats catching me up.

Try doing a practice run on paper with some data to see how you would store and retrieve it.

If you know how to retrieve all the data and print it -- it shouldn't be too hard to think about how to retireve all the data, and print the ones that meet a condition.
code:
for each element in array:
   print array

for each element in array:
   if element is one we want to print:
      print array
The forum isn't the best place to get this sort of interactive help -- someone may take pity on you and walk you through this on irc (irc.synirc.net/#cobol).

Soldat
Jan 22, 2008
my name is soldat and I get respect, your cash and your jewelery is what I expect
Could anybody suggest some toy/ for learning purposes
implementation(interpreter and/or compiler) of scheme (or part of
it) written in c or c++?

I need to look in more on the relationship between the 2 languages on a really basic level, and something like a really basic interpreter or compiler would help. Something even like tinyscheme is a little bit complex for me.

Moetic Justice
Feb 14, 2004

by Fistgrrl
I'm taking my first C class right now, and it's mostly going well, but there's one thing I can't figure out: how do I take keyboard input without waiting for the user to press enter (along the lines of "press any key to continue")? Am I missing something obscenely obvious?

haveblue
Aug 15, 2005



Toilet Rascal

Jungle Bus posted:

I'm taking my first C class right now, and it's mostly going well, but there's one thing I can't figure out: how do I take keyboard input without waiting for the user to press enter (along the lines of "press any key to continue")? Am I missing something obscenely obvious?

getc()

Kessel
Mar 6, 2007

I'm trying to read in the tag size for an ID3v2.3 tag. In the file, it's specified as four bytes with the first bit of each byte set to zero and ignored; that is,

0xxxxxxx 0xxxxxxx 0xxxxxxx 0xxxxxxx

So 257 would be

00000000 00000000 00000002 00000001

I can read the four bytes byte by byte, but what should I do to turn them into an integer value that I can use? The ignoring of the leading bit is tripping me up.

6174
Dec 4, 2004

Kessel posted:

I'm trying to read in the tag size for an ID3v2.3 tag. In the file, it's specified as four bytes with the first bit of each byte set to zero and ignored; that is,

0xxxxxxx 0xxxxxxx 0xxxxxxx 0xxxxxxx

So 257 would be

00000000 00000000 00000002 00000001

I can read the four bytes byte by byte, but what should I do to turn them into an integer value that I can use? The ignoring of the leading bit is tripping me up.

That 2 in your example should be a 1, I think.

Assuming I counted correctly, the number would be:
byte1 * 2^22 + byte2 * 2^15 + byte3 * 2^8 + byte4

Kessel
Mar 6, 2007

6174 posted:

That 2 in your example should be a 1, I think.

Assuming I counted correctly, the number would be:
byte1 * 2^22 + byte2 * 2^15 + byte3 * 2^8 + byte4
No, that's precisely the problem.

The last two bytes are O0000002 O0000001, where I've used O to indicate ignored bits. So put them together and you actually get 20000001, which is 2*128 + 1 = 257.

It's that each first bit is ignored that trips me up. I think your multiplication works, though - I'll try it out.

vvvvvvvv Thanks.

Kessel fucked around with this message at 00:49 on Apr 24, 2008

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Just pretend they're 7 digits wide instead of 8. So instead of 0,8,16,24, the sequence is 0,7,14,21

0 * 2^24 + 0 * 2^16 + 2 * 2^8 + 1 * 2^0 = 513 (wrong)
0 * 2^21 + 0 * 2^14 + 2 * 2^7 + 1 * 2^0 = 257 (right)

Moetic Justice
Feb 14, 2004

by Fistgrrl

HB posted:

getc()

That's not working for me. I have something like:

code:
while (foo != 'q' && foo != 'Q') {
     foo = getc (stdin);
     switch (foo) {
          ...
     }
}
And when I type something, it patiently accepts input until I hit Enter, and then executes the switch statement for each character individually.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
There's no way to do what you want in standard C. If stdin is interactive, every input function will block until a newline or EOF is entered. (Or until some kind of signal is received)

TheSleeper
Feb 20, 2003

Jungle Bus posted:

That's not working for me. I have something like:

code:
while (foo != 'q' && foo != 'Q') {
     foo = getc (stdin);
     switch (foo) {
          ...
     }
}
And when I type something, it patiently accepts input until I hit Enter, and then executes the switch statement for each character individually.

When I tried to something like that back when I was in college, I wound up having to search MSDN forever and as I can recall I had to either use something like __getc() and/or some kind of voodoo to force stdin not to wait.

Edit: I think I've found it. Either getch() or getche() in conio.h. You may or may not need to preface the function names with underscores. These functions read characters directly from the console without buffering at all. The first will read without echoing(when you press "y" the program will see it but it won't show on the screen) while the second does echo.

TheSleeper fucked around with this message at 02:18 on Apr 24, 2008

Kessel
Mar 6, 2007

When I compile in gcc 2.95.2 I get the following:
code:
Undefined                       first referenced
 symbol                             in file
pow                                 project5.o
ld: fatal: Symbol referencing errors. No output written to project5.out
collect2: ld returned 1 exit status
make: *** [project] Error 1
I am using the pow function from math.h in my program. What should I change to get this out of the way?

I've already tried adding Math. in front of every pow() call. I didn't notice this error earlier since I was compiling on a machine with gcc 4.0.1.

edit - Yes, math.h is included.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Kessel posted:

I am using the pow function from math.h in my program. What should I change to get this out of the way?

Either compile with optimizations or add "-lm" to the compiler command line so you link with the math library. You'll probably want -lm after all your source files.

Kessel
Mar 6, 2007

ShoulderDaemon posted:

Either compile with optimizations or add "-lm" to the compiler command line so you link with the math library. You'll probably want -lm after all your source files.
Thanks! That fixed it.

Out of interest, why does this happen with the math library, but not when I include things like ctype.h?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Kessel posted:

Thanks! That fixed it.

Out of interest, why does this happen with the math library, but not when I include things like ctype.h?

.h files are not libraries, they are header files describing an interface. The interface belongs to a library. One library, libc, which many header files describe various interfaces for, is always linked in by default because it's nearly impossible to program without it. No other libraries are linked in by default, so you have to pass them to the compiler yourself. Header files don't have a way to tell the compiler what libraries they need; you just have to know or check the documentation for the header you're using.

libm is a weird case because many of its functions can be replaced by direct invocation of the FPU, so if you turn on optimizations in your compiler it may remove the need to link to libm.

6174
Dec 4, 2004

Kessel posted:

The last two bytes are O0000002 O0000001, where I've used O to indicate ignored bits. So put them together and you actually get 20000001, which is 2*128 + 1 = 257.

It appears you've got this sorted out from later posts, but if you are going to specify a byte by putting zeros to pad out to eight positions, then having a "2" doesn't logically make sense since binary has only zeros and ones. That is what my comment about the 2 was previously. Also apparently I can't count, but JoeNotCharles can.

Adbot
ADBOT LOVES YOU

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Yeah, but I didn't even notice the 2 made no sense, so I fail at computer science.

I assume it was supposed to be "00000010 00000001"

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply