How do i write and call this type - c++

I'm having a problem here and the problem is that the hService is 0
I'm having or creating the Windows Form application and I've put:
HSERVICE hService=0;
BOOL fSuccess=EXIT_SUCCESS;
if(Wfs_Startup())
{
// This returns a successful startup even if I write something here
// to be displayed by a textbox it does. That means the Startup is ok.
if(Wfs_Open(&hService))
{
// What ever I put here doesn't show on a textbox and the application jumps to
// the exception of this block which means there's a problem here, at first I
// thought it was because of no corresponding logical name on a registry but what
// I found out was that if I check below the Startup block and check the hService
// it's 0 so it doesn't receive the correct data from the startup.
}
}
so I took that from "I will say" a function that is written like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdDLine,
int nShowCmd)
{
//THE CODE BLOCK IS THE SAME AS THE ABOVE THAT GOES HERE.
}
So I took the inside code block to FormLoad but this is giving me the above mentioned problem, can I have both the FormLoad and the WinMain? Because I tried to have the WinMain but there was an error I think it said there can't be two Main or something like that. Also how can I call the WinMain within FormLoad as when I tried it required the arguments to be included and the main problem is that I can't add the arguments in a FormLoad.
any suggestion to how can I resolve this problem?
Sorry but don't know the easy way to explain this. But keep in mind the problem that the hService returns 0.
Ok just to simplify this question:
How to call this type of Function that's starts with HRESULT at the beginning please check above. What I want is to fire that Function, I think that will simplify this even though there's another question about calling it on FormLoad but first I want to know how to call/fire that kind of Function?
The problem is that the IDE says I can not have the int WinMain on Windows Form because its already has something like that but when I'm working with command its works. I can have WinMain and Main but can't have WinMain and FormLoad not inside but within the project or page. To understand please create a Windows Form App and try typing the int WinMain code you will see what's my problem here.

I have found the great tutorial that gives a good reason why I'm getting this problem:
https://msdn.microsoft.com/en-us/library/bb384843.aspx

Related

Running C++ DLL with rundll32 - Missing entry

So I'm trying to use rundll to test and run my C++ written DLL , and i've followed the guidelines here and on other sites and the entry point looks like this:
__declspec(dllexport) void CALLBACK entry(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
//do stuff
}
But the rundll process prompts me with an error message box saying "Error in C:\path\to\DLL Missing entry: entry"
Attempted execution command: "rundll32 C:\path\to\DLL, entry".
You can double-check by running Dumpbin.exe /exports on your DLL to see the actual exported name. Which right now is not entry, as the error message tells you.
You need to prefix extern "C" to the declaration to disable C++ name mangling.
Good enough for a 64-bit executable, but that turns it into _entry#16 if this is a 32-bit DLL, still not quite good enough to keep rundll32 happy. Use Project + Add New Item, Code node, pick the "Module-Definition File (.def)" item template. And make it look like this:
EXPORTS
entry = entry

GetModuleHandle(NULL) vs hInstance

When programming using the Windows API, I've always made the HINSTANCE from WinMain a global variable immediately. If I want to make an OK button, I'd do it like so (given global HINSTANCE g_hInstance):
return CreateWindow("BUTTON", "OK", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 10, 10, 100, 30, exampleParentWindow, EXAMPLECHILDID, g_hInstance, NULL);
but lately I've been seeing the instance handle determined without having to be passed as a parameter or clogging up the global namespace, using a call to GetModuleHandle(NULL)*. So, the example above would look like this:
return CreateWindow("BUTTON", "OK", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 10, 10, 100, 30, exampleParentWindow, EXAMPLECHILDID, GetModuleHandle(NULL), NULL);
*If your compiler supports it, you can write GetModuleHandle(nullptr) and the statement will have the same result.
What's the advantage (if any) of calling GetModuleHandle(NULL) over explicitly specifying the instance handle?
Fine Print: I know this has an answer, but it has not been phrased as its own question on StackOverflow.
In an EXE, it does not make any difference. hInstance from WinMain() and GetModuleHandle(NULL) both refer to the same HINSTANCE (the module of the .exe file). But it does make a difference if you are creating windows inside of a DLL instead, since you have to use the DLL's hInstance but GetModuleHandle(NULL) will still return the HINSTANCE of the EXE that loaded the DLL.
HMODULE WINAPI GetModuleHandle( _In_opt_ LPCTSTR lpModuleName );
Give the module handle of the module name passed.If you are passing NULL, the you get the module handle of the EXE which is currently running.
If you specifically name the module name, you get the module handle of that dll which is mapped to the process address space.
The use is that when you are trying to call a function exported by the dll, or trying to use a dialog template in side that dll.At that time if you use the HMODULE returned form GetMoudleHandle(NULL) your code wont work.
Just to add my two-cents to these answers. In case you need to get the module handle from within a DLL (and don't want to, or can't save it in a global variable from the DllMain call) you can use this function to get it instead:
HMODULE getThisModuleHandle()
{
//Returns module handle where this function is running in: EXE or DLL
HMODULE hModule = NULL;
::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)getThisModuleHandle, &hModule);
return hModule;
}
One potential gain you get from using GetModuleHandle(NULL) over directly using the WinMain HINSTANCE comes more from architecture. If you want to provide a platform-independent system that runs on linux/windows/whatever you can have a layer that does platform-dependent translations. If that's the case you don't want platform dependent objects such as HINSTANCE showing up in the main application code. So, to circumvent that platform-dependence I put GetModuleHandle(NULL) in the constructor of the platform-dependent class which has the same effect that direct use of the WinMain HINSTANCE does but that abstracts that specific functionality out of the main codebase itself.

How to convert an Integer Handle to HWND

I am writing a small test program, and for that i require the handle of an edit control. I have copied the value of the handle from Spy ++ (lets say 000A0B40).
So i did the following
#define editControlHandle 0x000A0B40
int *intHandle;
intHandle=(int*)editControlHandle;
HWND handle=(HWND)intHandle;
int textlength=GetWindowTextLength(handle);
I also did a getlasterror and it gave me error_success.
GetWindowTextLength cannot retrieve the length of the text of an edit control in another application.
Documentation

another win32 problem

Having a problem here with creating a child window with c++ and win32 api.
If i check the getLastError function its returning "87" but i dont know what that means.
For what i know my code does not contain errors, can someone take a look at my code and help me figure out whats wrong with it.
(This is in the WinProc WM_CREATE section.)
HWND hChildWindow = CreateWindowEx(WS_EX_CLIENTEDGE,0,NULL,WS_OVERLAPPEDWINDOW|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,hwnd,0,GetModuleHandle(0),NULL);
if(!hChildWindow)
{
char text[256];
int errormsg = (int)GetLastError();
sprintf(text,"Error# %i",errormsg);
MessageBox(0,text,"Error",MB_OK|MB_ICONEXCLAMATION);
return false;
}
87 = Invalid Parameter - be aware that you can use FormatMessage to get a string message from an error code.
The 2nd parameter to CreateWindowEx is a window class (either string or ATOM). Obviously NULL is not a valid value.
P.S.
For what i know my code does not
contain errors...
Beware of such a loud phrases. When something doesn't work everything should be checked carefully. Otherwise you may just accuse something/someone without any good for solving the problem. Check everything vs standard/documentation/specifications/etc. before you make any judgement.
A quick look through the System Error Codes reference indicates ERROR_INVALID_PARAMETER. You're most likely passing in an invalid combination of styles/flags to your window.

how do i make this so everything can use it? C++

im somewhat new to c++ so i don't know how to do this but my main point in recoding this is to incress the speed of my program, i have been coding a project
and my code is:
HWND hWnd = FindWindow(NULL, L"*window's name*");
DWORD th32ProcId;
HANDLE hProc;
GetWindowThreadProcessId(hWnd, &th32ProcId);
hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, 0, th32ProcId);
so i have about 20 functions that all use that at the start of each time i run them and the value will never change so is there some way i can declare and then set it at the value of what it finds?
my code is set up like this
one main file int main() and it's just set on a loop and it keeps retesting and calls the other functions and everything else is in a void name() and i have 2 int name()
im using VC++ 2008.
edit
no :| i just want a way i can share thoses values with all of the program.
If I understand your question correctly you want to implement some kind of caching. That would be fine and you can create a cahe with an std::map.
You would probably have something like:
std::map<std::string, HANDLE> cacheMap;
You can then check the cacheMap to see if a result exists. It if does exist you don't need to call the function, if it does not exist you would then call the function and add the result to your map.