|
Member 4705538 wrote: By properly, i mean when i look at pieces of code, i always only see a something.h, never a something.cpp, in my own code i always had to add the supplier.h to the client.h and the supplier.cpp to the client.cpp, but was wondering if there is a better way of doing it?
Short answer - yes, there are better ways of doing it.
Longer answer - there are two approaches you could consider. Firstly, as I suggested earlier, make a library (static or dynamic, static's probably easier)[^] out of the supplier.cpp file and get the code that uses it to link against that library. Secondly, you could just add the supplier.cpp file to the Visual Studio project that contains client.cpp. That will automatically include the compiled object files of supplier.cpp and client.cpp in the final executable, which should mean it links and build successfully.
HTH!!!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
lack of impleation file
Like C++ more
|
|
|
|
|
I want to read the records of some students such as their age, names, marks in tests etc. from a text file.
Can these records be saved directly from a file to a structure?
(I mean we are not taking any input from the user.)
Any other way to do this job?
|
|
|
|
|
I think the code will help in your problem if the file is made by filling the same structure.
Try this
int nNumberOfBytes = 0;
bool bRead = ReadFile(
hMyFile,
(LPVOID)MyStruct,
sizeof(MyStruct),
&nNumberOfBytes,
NULL);
Harsh
|
|
|
|
|
Razanust wrote: Can these records be saved directly from a file to a structure?
Yes, of course.
Razanust wrote: Any other way to do this job?
There are other ways (such as reading from a database...).
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
[My articles]
|
|
|
|
|
Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening?
Thanks, David Kirby
/*-----------------------------------------------------------------------------------------------*/
#include "CXBOXController.h"
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <cstdlib>
typedef bool (*Type_InitMotoBee)();
typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo);
typedef void (*Type_Digital_IO)(int *inputs, int outputs);
using namespace std;
HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll"));
CXBOXController* Player1;
int main(int argc, char* argv[])
{
Player1 = new CXBOXController(1);
system("Color 02");
system("title LANbot Host - Local Motor Control Trial");
cout << " __ _ __ _ _ |\n"
<< " / / /_\\ /\\ \\ \\ |__ ___ | |_ |\n"
<< " / / //_\\\\ / \\/ / '_ \\ / _ \\| __| |\n"
<< " / /___/ _ \\/ /\\ /| |_) | (_) | |_ |\n"
<< " \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \\___/ \\__| 2009|\n"
<< "--------------------------------------------\n\n";
//End Title Bar
if(MHtbDLL != NULL){cout << "\tMtb.dll Loaded Successfuly...";}
if(MHtbDLL == NULL)
{
cout << "\tMtb.dll did NOT load Successfuly!\n"
<< "\tCheck to see that Mtb.dll is in the same\n"
<< "\tdirectory as this executable.\n\n"
<< "\tPress [Enter] to Continue...";
cin.get();
}
Sleep(1000); system("cls");
int speed = 1;
int gR = 128;
int button=0;
Type_InitMotoBee InitMotoBee;
Type_SetMotors SetMotors;
Type_Digital_IO Digital_IO;
InitMotoBee = (Type_InitMotoBee)GetProcAddress(MHtbDLL, "InitMotoBee");
SetMotors = (Type_SetMotors)GetProcAddress(MHtbDLL, "SetMotors");
Digital_IO = (Type_Digital_IO)GetProcAddress(MHtbDLL, "Digital_IO");
InitMotoBee();
while(true)
{
if(Player1->IsConnected())
{
//Set both motors Foward
if(Player1->GetState().Gamepad.sThumbLY > 5001
&& Player1->GetState().Gamepad.sThumbLX > -4999
&& Player1->GetState().Gamepad.sThumbLX < 4999
&& button==0)
{
SetMotors(1,gR,0,0,1,gR,0,0,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLY > 5001
&& Player1->GetState().Gamepad.sThumbLX > -4999
&& Player1->GetState().Gamepad.sThumbLX < 4999
&& button==1)
{
cout << "\n\n\t\t\tFOWARD\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Set Right motor foward, turn Left
if(Player1->GetState().Gamepad.sThumbLY > 5001
&& Player1->GetState().Gamepad.sThumbLX < -4999
&& button==0)
{
SetMotors(1,gR,0,0,0,0,0,0,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLY > 5001
&& Player1->GetState().Gamepad.sThumbLX < -4999
&& button==1)
{
cout << "\n\n\t\t\tFOWARD, LEFT TURN\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Set Left Motor Foward, turn Right.
if(Player1->GetState().Gamepad.sThumbLY > 5001
&& Player1->GetState().Gamepad.sThumbLX > 4999
&& button==0)
{
SetMotors(0,0,0,0,1,gR,0,0,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLY > 5001
&& Player1->GetState().Gamepad.sThumbLX > 4999
&& button==1)
{
cout << "\n\n\t\t\tFOWARD, TURN RIGHT\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Set both motors in reverse
if(Player1->GetState().Gamepad.sThumbLY < -5001
&& Player1->GetState().Gamepad.sThumbLX > -4999
&& Player1->GetState().Gamepad.sThumbLX < 4999
&& button==0)
{
SetMotors(0,0,1,128,0,0,1,128,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLY < -5001
&& Player1->GetState().Gamepad.sThumbLX > -4999
&& Player1->GetState().Gamepad.sThumbLX < 4999
&& button==1)
{
cout << "\n\n\t\t\tREVERSE\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Reverse Right motor
if(Player1->GetState().Gamepad.sThumbLY < -5001
&& Player1->GetState().Gamepad.sThumbLX < -4999
&& button==0)
{
SetMotors(0,0,1,128,0,0,0,0,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLY < -5001
&& Player1->GetState().Gamepad.sThumbLX < -4999
&& button==1)
{
cout << "\n\n\t\t\tREVERSE, TURN LEFT\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Reverse Left Motor
if(Player1->GetState().Gamepad.sThumbLY < -5001
&& Player1->GetState().Gamepad.sThumbLX > 4999
&& button==0)
{
SetMotors(0,0,0,0,0,0,1,128,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLY < -5001
&& Player1->GetState().Gamepad.sThumbLX > 4999
&& button==1)
{
cout << "\n\n\t\t\tREVERSE, TURN RIGHT\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Spin Right
if(Player1->GetState().Gamepad.sThumbLX > 5001
&& Player1->GetState().Gamepad.sThumbLY < 4999
&& Player1->GetState().Gamepad.sThumbLY > -4999
&& button==0)
{
SetMotors(0,0,1,gR,1,gR,0,0,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLX > 5001
&& Player1->GetState().Gamepad.sThumbLY < 4999
&& Player1->GetState().Gamepad.sThumbLY > -4999
&& button==1)
{
cout << "\n\n\t\t\tSPIN RIGHT\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Spin Left
if(Player1->GetState().Gamepad.sThumbLX < -5001
&& Player1->GetState().Gamepad.sThumbLY < 4999
&& Player1->GetState().Gamepad.sThumbLY > -4999
&& button==0)
{
SetMotors(1,gR,0,0,0,0,1,gR,0);
button=1;
while(Player1->GetState().Gamepad.sThumbLX < -5001
&& Player1->GetState().Gamepad.sThumbLY < 4999
&& Player1->GetState().Gamepad.sThumbLY > -4999
&& button==1)
{
cout << "\n\n\t\t\tSPIN LEFT\n";
system("cls");
}
button=0;
SetMotors(0,0,0,0,0,0,0,0,0);
}
//Angle Servo up
if(Player1->GetState().Gamepad.sThumbRY > 5000)
{
cout << "\n\n\t\t\tCAMERA UP\n";
system("cls");
}
//Angle Servo Down
if(Player1->GetState().Gamepad.sThumbRY < -5000)
{
cout << "\n\n\t\t\tCAMERA DOWN\n";
system("cls");
}
//Stop Motors
if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB)
{
SetMotors(0,0,0,0,0,0,0,0,0);
cout << "STOPPED";
system("cls");
}
//Gear One
if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
{
speed=1; gR=128;
while(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_A)
{
cout << "\n\n\t\t\tShifting. You are now in gear " << speed << ".\n";
system("cls");
}
}
//Gear Two
if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
{
speed=2; gR=189;
while(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B)
{
cout << "\n\n\t\t\tShifting. You are now in gear " << speed << ".\n";
system("cls");
}
}
//Gear Three
if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_Y)
{
speed=3; gR=250;
while(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_Y)
{
cout << "\n\n\t\t\tShifting. You are now in gear " << speed << ".\n";
system("cls");
}
}
/*if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_START)
{
SetMotors(0,0,0,0,0,0,0,0,0);
int brake=1;
cout << "BRAKE";
while(brake==1)
{
if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_START)
{
brake++;
}
}
system("cls");
}*/
//Exit Loop (break;)
if(Player1->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_BACK)
{
system("cls");
cout << "\n\n\tUnloading library...\n";
FreeLibrary(MHtbDLL);
Sleep(1000);
if(MHtbDLL!=NULL){cout << "\n\n\tMtb.dll Did NOT Unload Successfuly!";}
if(MHtbDLL==NULL){cout << "\n\n\tMtb.dll Unloaded Successfuly...";}
Sleep(1000);
break;
}
}
//Controller Not present
else
{
cout << "\n\t\t\tWhere is the controller? o.0\n";
cout << "\t\t\t Press [ENTER] To Exit.";
cin.get();
system("cls");
cout << "\n\n\tUnloading library...\n";
FreeLibrary(MHtbDLL);
Sleep(1000);
if(MHtbDLL!=NULL){cout << "\n\n\tMtb.dll Did NOT Unload Successfuly!";}
if(MHtbDLL==NULL){cout << "\n\n\tMtb.dll Unloaded Successfuly...";}
Sleep(1000);
break;
}
}
delete(Player1);
return( 0 );
}
<code></code><code></code><pre></pre>
|
|
|
|
|
This would be too much work for somebody trying to help you.
It is for me. So you should consider drilling this down to a more specific area.
These are some comments from me.
Don't use new unless you really need it.
You are using too many system function calls.
The code is not properly indented.
You are not checking return values for errors.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Thanks for the reply. Im getting and error with these...
typedef bool (*Type_InitMotoBee)();
typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo);
typedef void (*Type_Digital_IO)(int *inputs, int outputs);
HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll"));
Type_InitMotoBee InitMotoBee;
Type_SetMotors SetMotors;
Type_Digital_IO Digital_IO;
InitMotoBee = (Type_InitMotoBee)GetProcAddress(MHtbDLL, "InitMotoBee");
SetMotors = (Type_SetMotors)GetProcAddress(MHtbDLL, "SetMotors");
Digital_IO = (Type_Digital_IO)GetProcAddress(MHtbDLL, "Digital_IO");
This is where the program calls the DLL, makes pointers, etc.
After I use SetMotors(); so many times, the program is terminated for an access violation... I cant figure it out. If i comment out everything that envolves the DLL, my program works perfectly.
Thanks,
David
|
|
|
|
|
Check for any uninitialized or dangling pointers inside SetMotors.
Check if there is any difference in behavior in Debug and Release builds.
Also set the warning level to the highest level in the project properties.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
How exactly would i Check for dangling pointers in SetMotors? If its any help, I can tell you that When i run the app in 32bit windows xp OS, the app closes after two successful SetMotors(); commands. In 64bit windows vista OS desktop, the app runs well for quite a while before closing for the same reason. I'm checking for release/debug differences now.
|
|
|
|
|
Are you allocating memory dynamically?
If so, check if they are being released properly.
Also, check for buffer overflows.
If you're using string functions, use the secure version that end with _s.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Sorry, I'm new at including DLL's. I have seen "Buffer Overflow" show up as an error, but from what i can tell I fixed that. I'm pretty sure that when that occured, the compiler (Visual Studio 2008) failed to compile the app and gave me the buffer overflow error. that is no longer the case. I am not allocating memory dynamically for the DLL. there is no difference between Release and Debug. It is handling itself after I call the DLL, make the pointers, and GetProcAddress. I just use the functions...
the functions are used as follows:
SetMotors(x,x,x,x,x,x,x,x,x)
InitMotoBee();
|
|
|
|
|
It seems that the access violation is due to Dangling pointers. These kind of error will not be repeatable 100% ,since will be depending on size of your RAM ( when your OS tries to get back the memory which is assigned to some one else).If you have Dev-Partner with your studio , you can find the code problems or try to make sure that there is delete() for every new().You can use run time checker for this task , It's evaluation version may help you in Finding leaks.
It's not enough to be the best, when you have capability to be great....
|
|
|
|
|
I want to hook keboard events of an application, and that hook is associated with a ThreadID - NOT global hook. So I put the KeyboardProc() , and the InstallKeyboardHook() functions, BOTH in a DLL.
In the DllMain() function, I call to install the hook like this:
KeyboardProc()
{
//something
}
InstallKeyboardHook()
{
//something
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD dwReason,
LPVOID lpReserved
)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
InstallKeyboardHook();
break;
case DLL_PROCESS_DETACH:
UnhookWindowsHookEx(hHook);
break;
}
return TRUE;
}
The DLL is loaded by the desired hook application.
It's loaded, the SetWindowsHookEx() success, but nothing happen when i press some key in the application main window.
So why ? and is it wrong when i put the install code and hook proc both in the same DLL for self install ?
Thanks very much!
|
|
|
|
|
Ensure that the thread id belongs to the same process.
How are you getting the thread id?
Some hooks can only be global.
If you're installing the low level keyboard hook, it can only be global.
Look at the remarks section of the SetWindowsHookEx[^] function.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
I'm using keyboard hook, not the low level version.
And I find out the thread ID by the tool help functions, like this:
dwPID=GetCurrentProcessId();
hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
te32.dwSize=sizeof(THREADENTRY32);
bResult=Thread32First(hSnapshot,&te32);
while((te32.th32OwnerProcessID!=dwPID)&&bResult)
{
te32.dwSize=sizeof(THREADENTRY32);
bResult=Thread32Next(hSnapshot,&te32);
}
But it didn't work !
|
|
|
|
|
Why don't you use GetCurrentThreadId[^] to get the current thread id.
I would recommend you put the hooking code into the application directly and get it working before writing it as part of the DLL.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
I want to hook the main thread of the application. But I'm not sure that call GetCurrentThreadID() in the context of a DLL will get me the desired thread.
In fact I tried that API, but the result is still the same.
Thank you!
|
|
|
|
|
Hi, I wrote an ActiveX in Visual Basic 6 that have a form with the purpose to act like a pop-up menu and to be used with any language. The form contains a WebBrowser. I have two situations:
1. When the form is shown in a modal way it works fine in both VB and VC++;
2. When the form is shown in a non modal way it only works in VB, the VC++ test app doesn't show the form and when the test app is closed the error message appears: "The instruction at 0x43dd085d referenced the memory at 0x00000138. The memory cannot be read" (translated from brazilian portuguese). A form with no WebBrowser doesn't crash, but doesn't show the form anyway!
The objective is make the form appears like a pop-up menu, so the modal way of showing the form forces the user to select an option or close the form like closing a window, pressing the close button, doens't allowing a click outside the activex form.
How can it works in a non modal way to any language?
Thanks, best regards!
modified on Tuesday, June 30, 2009 3:24 PM
|
|
|
|
|
Hi,
May i know why we need to use only 72 in the below function
lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
I had a look in msdn where it was not explained...
|
|
|
|
|
one point = 1/72 of an inch
|
|
|
|
|
if u don"t mind can u be a little more clear...
|
|
|
|
|
GetDeviceCaps(hDC, LOGPIXELSY) gives you the number of pixels per inch for the device. (pixels/inch)
there are 72 points in an inch. (points/inch)
the other variable is points.
so (points * (pixels / inch)) / (points / inch) = pixels = height of text.
|
|
|
|
|
Are you asking What is a point?[^] Usually we express the size of the font in points. See the link.
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
kumar sanghvi wrote: if u don"t mind can u be a little more clear...
Point (typography)[^]
Best Wishes,
-David Delaune
|
|
|
|
|