Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an example of reading from an json with only a key that has an array of strings. #354

Closed
AraHaan opened this issue Nov 5, 2016 · 5 comments

Comments

@AraHaan
Copy link

AraHaan commented Nov 5, 2016

So, I have a json that is somewhat like this:

{
	"detectlist": [
		"Cheat Engine 0.1",
		"Cheat Engine 0.2",
		"Cheat Engine 0.3",
		"Cheat Engine 0.4",
		"Cheat Engine 0.5",
		"Cheat Engine 0.6",
		"Cheat Engine 0.7",
		"Cheat Engine 0.8",
		"Cheat Engine 0.9",
		"Cheat Engine 1.0",
		"Cheat Engine 1.1",
		"Cheat Engine 1.2",
		"Cheat Engine 1.3",
		"Cheat Engine 1.4",
		"Cheat Engine 1.5",
		"Cheat Engine 1.6",
		"Cheat Engine 1.7",
		"Cheat Engine 1.8",
		"Cheat Engine 1.9",
		"Cheat Engine 2.0",
		"Cheat Engine 2.1",
		"Cheat Engine 2.2",
		"Cheat Engine 2.3",
		"Cheat Engine 2.4",
		"Cheat Engine 2.5",
		"Cheat Engine 2.6",
		"Cheat Engine 2.7",
		"Cheat Engine 2.8",
		"Cheat Engine 2.9",
		"Cheat Engine 3.0",
		"Cheat Engine 3.1",
		"Cheat Engine 3.2",
		"Cheat Engine 3.3",
		"Cheat Engine 3.4",
		"Cheat Engine 3.5",
		"Cheat Engine 3.6",
		"Cheat Engine 3.7",
		"Cheat Engine 3.8",
		"Cheat Engine 3.9",
		"Cheat Engine 4.0",
		"Cheat Engine 4.1",
		"Cheat Engine 4.2",
		"Cheat Engine 4.3",
		"Cheat Engine 4.4",
		"Cheat Engine 4.5",
		"Cheat Engine 4.6",
		"Cheat Engine 4.7",
		"Cheat Engine 4.8",
		"Cheat Engine 4.9",
		"Cheat Engine 5.0",
		"Cheat Engine 5.1",
		"Cheat Engine 5.2",
		"Cheat Engine 5.3",
		"Cheat Engine 5.4",
		"Cheat Engine 5.5",
		"Cheat Engine 5.6",
		"Cheat Engine 5.7",
		"Cheat Engine 5.8",
		"Cheat Engine 5.9",
		"Cheat Engine 6.0",
		"Cheat Engine 6.1",
		"Cheat Engine 6.2",
		"Cheat Engine 6.3",
		"Cheat Engine 6.4",
		"Cheat Engine 6.5",
		"Cheat Engine 6.6",
		"Cheat Engine 6.7",
		"Cheat Engine 6.8",
		"Cheat Engine 6.9",
		"Cheat Engine 7.0",
		"OllyDBG",
		"PEiD v0.95"
	]
}

What I want to do is iterate through that list and with each string in it I want to use the FindWindow function from kernel32.dll to check if a window with a string in the list is open or not.

Here is how I would like the strings to look like if output to a console (with new lines after each one):

Cheat Engine 0.1
Cheat Engine 0.2
Cheat Engine 0.3
Cheat Engine 0.4
Cheat Engine 0.5
Cheat Engine 0.6
Cheat Engine 0.7
Cheat Engine 0.8
Cheat Engine 0.9
Cheat Engine 1.0
Cheat Engine 1.1
Cheat Engine 1.2
Cheat Engine 1.3
Cheat Engine 1.4
Cheat Engine 1.5
Cheat Engine 1.6
Cheat Engine 1.7
Cheat Engine 1.8
Cheat Engine 1.9
Cheat Engine 2.0
Cheat Engine 2.1
Cheat Engine 2.2
Cheat Engine 2.3
Cheat Engine 2.4
Cheat Engine 2.5
Cheat Engine 2.6
Cheat Engine 2.7
Cheat Engine 2.8
Cheat Engine 2.9
Cheat Engine 3.0
Cheat Engine 3.1
Cheat Engine 3.2
Cheat Engine 3.3
Cheat Engine 3.4
Cheat Engine 3.5
Cheat Engine 3.6
Cheat Engine 3.7
Cheat Engine 3.8
Cheat Engine 3.9
Cheat Engine 4.0
Cheat Engine 4.1
Cheat Engine 4.2
Cheat Engine 4.3
Cheat Engine 4.4
Cheat Engine 4.5
Cheat Engine 4.6
Cheat Engine 4.7
Cheat Engine 4.8
Cheat Engine 4.9
Cheat Engine 5.0
Cheat Engine 5.1
Cheat Engine 5.2
Cheat Engine 5.3
Cheat Engine 5.4
Cheat Engine 5.5
Cheat Engine 5.6
Cheat Engine 5.7
Cheat Engine 5.8
Cheat Engine 5.9
Cheat Engine 6.0
Cheat Engine 6.1
Cheat Engine 6.2
Cheat Engine 6.3
Cheat Engine 6.4
Cheat Engine 6.5
Cheat Engine 6.6
Cheat Engine 6.7
Cheat Engine 6.8
Cheat Engine 6.9
Cheat Engine 7.0
OllyDBG
PEiD v0.95

Any idea what I can do to anchieve this? I have the following code so far:

nlohmann::json GetJsonData()
{
	std::ifstream in("VB-Trap\\checkwindow.json");
	//error when loading Window data.
	if(!in.is_open())
	{
		return nullptr;
	}
	nlohmann::json file;
	file << in;
	in.close();
	return file;
}

VBTREXTERN int __cdecl DetectWithJSONDB()
{
	nlohmann::json jsondb = GetJsonData();
	if (jsondb != nullptr)
	{
		//TODO: Iterate through this data somehow.
		//TODO: Within the iteration for each string in the Array invoke the ``Window_Scan`` function to check for if the Window(s) are open.
		/*
			Note: If NULL is returned from the iterator list then the function will return NULL.
			Otherwise the function Will return -1 when any of the Windows in the list are open.
			
			If The File cannot be opened then -3 is returned.
		*/
		return NULL;
	}
	else
	{
		return -3;
	}
	return NULL;
}
@boguscoder
Copy link

boguscoder commented Nov 5, 2016

How about this:

#include <fstream>
#include <iostream>
#include "json.hpp"

using namespace nlohmann;

int main() {
    std::ifstream in("./test.json");
    json file = json::parse(in);

    try {
        auto& array = file.at("detectlist");

        for (auto&& val: array) {
            std::cout << val << std::endl;
        }
    } catch(std::exception&) {
        std::cout << "no luck" << std::endl;
    }
}

@AraHaan
Copy link
Author

AraHaan commented Nov 5, 2016

ok, now that works but all I have to do is now take the value for each one and convert it to a LPCSTR for use in FindWindow.

@nlohmann
Copy link
Owner

nlohmann commented Nov 7, 2016

Can this ticket be closed?

@boguscoder
Copy link

I'd say yes

@nlohmann
Copy link
Owner

nlohmann commented Nov 8, 2016

@AraHaan You can get a std::string from the library - in the code snippet above, you can write std::string my_string_val = val; to get this (or maybe check val.is_string() before).

From there follow the usual conversions.

I close the ticket, because the original question has been answered. If you need further help, please let me know.

@nlohmann nlohmann closed this as completed Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants