I need to import the dll function into my project and use it.
However, when importing it into my project with GetProcAddress() function it does not seem to work. Function returns null and the simple if check finishes the program.
I'm pretty sure there is some problem with my code, but I don't seem to be able to find it. Here is the code of Library.h file
#pragma once
#include <iostream>
#include <windows.h>
#include <string>
#include <cmath>
using namespace std;
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API extern "C" _declspec(dllexport)
#else
#define MY_DLL_API extern "C" _declspec(dllimport)
#endif
struct args
{
int Elements;
int sum;
int* Array;
};
DWORD WINAPI NoSynchroSum(LPVOID);
void Monitoring(int);
Library.cpp file
#include "pch.h"
#include "framework.h"
#include "Library.h"
using namespace std;
DWORD WINAPI NoSynchroSum(LPVOID arg)
{
args* _args = (args*)arg;
for (int i = 0; i < _args->Elements; i++)
{
_args->sum += _args->Array[i];
}
return 0;
}
void Monitoring(int Elements)
{
int ElementsAmount = Elements;
HANDLE handle;
int suspendCount = 1;
int sum = 0;
srand(time(0));
int* Array = new int[ElementsAmount];
for (int i = 0; i < ElementsAmount; i++)
{
Array[i] = rand() % 21 - 10;
}
args* _args = new args;
_args->Elements = ElementsAmount;
_args->sum = sum;
_args->Array = Array;
handle = CreateThread(NULL, 0, NoSynchroSum, (LPVOID)_args, NULL, NULL);
cout << "Counted Sum = " << sum << endl;
CloseHandle(handle);
delete[] Array;
delete _args;
}
And my other project code, where i'm trying to import and use the function
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
HINSTANCE hInstance = LoadLibrary(L"D:\\Visual Studio 2022\\repos\\LabOS_7_DLL\\x64\\Debug\\DLLibrary.dll");
if (!hInstance) {
std::cout << "Library not loaded.\n";
return 1;
}
void (*pMainFunc)(int);
pMainFunc = (void(*)(int))GetProcAddress(hInstance, "Monitoring"); // експорт функції створення потоку
if (!pMainFunc)
{
std::cout << "Function not loaded.\n";
return 1;
}
int ElementsAmount = 0;
while (true)
{
cout << "enter size of array: ";
cin >> ElementsAmount;
if (ElementsAmount < 100)
{
cout << "size of array must be >=100\n";
continue;
}
break;
}
pMainFunc(ElementsAmount);
FreeLibrary(hInstance);
return 0;
}
I test the code and get the GetLastError:127 ERROR_PROC_NOT_FOUND:The specified procedure could not be found.
It means that there is no exported function with that name.
I suggest you should use the following code in the Library.h:
MY_DLL_API DWORD WINAPI NoSynchroSum(LPVOID);
MY_DLL_API void Monitoring(int);
I've implemented the below code to calculate system idle time in ubuntu on xorg which is working fine:-
#include <X11/extensions/scrnsaver.h>
#include <stdio.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xinerama.h>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
uint getSystemIdleTime()
{
Display *dpy = XOpenDisplay(nullptr);
if (!dpy) {
return(0);
}
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
XScreenSaverInfo *info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
uint idletime= info->idle / 1000;
XFree(info);
XCloseDisplay(dpy);
return idletime;
}
int main()
{
int ans = getSystemIdleTime();
cout << ans << endl;
}
but I'm unable to find any method/API to calculate system idle in ubuntu on Wayland.
I am trying to make a fun program where it display random numbers, but I need to remove the scrollbar so it looks more convincing. I managed to make the program full screen but I can't remove the vertical scrollbar. Screenshot
Code:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
There are many ways, one of them is manipulating the size of the internal buffer of the console to have the same size of the window and then using ShowScrollBar function to remove the scrolls.
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hstdout, &csbi);
csbi.dwSize.X = csbi.dwMaximumWindowSize.X;
csbi.dwSize.Y = csbi.dwMaximumWindowSize.Y;
SetConsoleScreenBufferSize(hstdout, csbi.dwSize);
HWND x = GetConsoleWindow();
ShowScrollBar(x, SB_BOTH, FALSE);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
Another way is to rely on conio.h or another C/C++ header/library which implements user interface functions.
I have written the code to access opencv(Mainly used for image processing) function from Scilab(numerical computation software). When I run bilder gateway file, I am encountering below error.
opencv_NormalizeHist.cpp:47:34: error: 'NormalizeHist' was not declared in this scope.
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
extern "C"
{
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "../common.h"
int opencv_NormalizeHist(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int iRows=0,iCols=0;
int *piAddr2 = NULL;
//checking input argument
CheckInputArgument(pvApiCtx, 2, 2);
CheckOutputArgument(pvApiCtx, 0, 0) ;
//Define histogram
Mat hist;
retrieveImage(hist,1);
double *factor;
//for value of factor
sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr2);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &iRows, &iCols ,&factor);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
NormalizeHist(hist, factor[0]); //Normalizing hist
string tempstring = type2str(hist.type());
char *checker;`enter code herek
checker = (char *)malloc(tempstring.size() + 1);
memcpy(checker, tempstring.c_str(), tempstring.size() + 1);
returnImage(checker,hist,1); //here, remove the temp as a parameter as it is not needed, and instead add 1 as the third parameter. 1 denotes that the first output argument will be this variable
free(checker); //free memory taken up by checker, i missed this earlier
//Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
//Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */
}
I have window_types.cpp
#include <string>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"
#include "../../utils/utilsf.h"
#include "../../extra_data/extra_data.h"
#include "window.h"
#include "window_types.h"
using namespace std;
using namespace windows;
message_window::message_window(string title,string con,int a[],int b[]) : bwindow(title, 300, 200, 300, 200,a,b){
vector <string> content_ordered;
string new_lines = "";
for (int x = 0;x < con.size();x++){
if (utilsf::cts(con[x]) == "/" and utilsf::cts(con[x]) == "r"){
content_ordered.push_back(new_lines);
new_lines = "";
x+=2;
}
new_lines = new_lines + utilsf::cts(con[x]);
}
SDL_Color textColor = {0, 0, 0};
TTF_Font *font = fonts::load_john_han(15);
int h = 0;
for (int x = 0;x < content_ordered.size();x++){
SDL_Surface* text_surface = TTF_RenderText_Solid(font,content_ordered[x].c_str(),textColor);
apply_surface(5,h,text_surface,content);
h += text_surface->h + 3;
}
}
/*void windows::message_window::start(string title,vector <string> content,int s){
int yp = 200;
int xp = 300;
int w = 100;
int h = 50;
int a[4];
int b[4];
a[0] = utilsf::randrange(0,256,s);
a[1] = utilsf::randrange(0,256,s);
a[2] = 200;
a[3] = 255;
b[0] = 200;
b[1] = utilsf::randrange(0,256,s);
b[2] = utilsf::randrange(0,256,s);
b[3] = 255;
bwindow(title,xp,yp,w,h,a,b);
}*/
void windows::message_window::test(){
}
message_window* get_message_window(string title,string msj,int s){
int a[4];
int b[4];
a[0] = utilsf::randrange(0,256,s);
a[1] = utilsf::randrange(0,256,s);
a[2] = 200;
a[3] = 255;
b[0] = 200;
b[1] = utilsf::randrange(0,256,s);
b[2] = utilsf::randrange(0,256,s);
b[3] = 255;
message_window *w = new message_window(title,msj,a,b);
return w;
}
Here window_types.h
/*
* window_types.h
*
* Created on: Aug 10, 2013
* Author: newtonis
*/
#ifndef WINDOW_TYPES_H_
#define WINDOW_TYPES_H_
#include <string>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"
#include "window.h"
#include "../../extra_data/extra_data.h"
using namespace std;
namespace windows{
class message_window : public bwindow{
private:
vector <string> message;
string title;
public:
message_window(string,string,int a[],int b[]);
void start(string,vector<string>,int);
void test();
};
message_window* get_message_window(string,string,int);
}
#endif /* WINDOW_TYPES_H_ */
And here I am calling the function get_message_window and I am getting the error "undefined reference to `windows::get_message_window(std::string, std::string, int)"
#include <vector>
#include <string>
#include "SDL/SDL.h"
#include "../utils/event.h"
#include "window/window.h"
#include "window/window_types.h"
#include "stage.h"
#include <iostream>
using namespace std;
using namespace windows;
stage::stage(){
int a[4];
a[0] = 100;
a[1] = 150;
a[2] = 200;
a[3] = 255;
int b[4];
b[0] = 245;
b[1] = 218;
b[2] = 129;
b[3] = 255;
add_window( new bwindow("Test window",20,20,200,200,a,b) );
//ERROR HERE!
message_window *w = windows::get_message_window("hello","hello",5);
add_window(w);
}
void stage::graphic_update(SDL_Surface* screen){
for (int x = 0;x < windws.size();x++){
windws[x]->graphic_update(screen);
}
}
void stage::logic_update(events *evs){
for (int x = 0;x < windws.size();x++){
windws[x]->logic_update(evs);
}
}
void stage::add_window(bwindow *win){
cout<<" Window titled "<<win->get_name()<<" added"<<endl;
windws.push_back(win);
}
Apparently, the function declaration in the header (which you haven't shown) puts the function into namespeace windows. But the function definition is in the global namespace. In other words, you've implemented one function, named ::get_message_window, but you are calling a different one, named windows::get_message_window.
Either take the declaration out of a namespace, or put the definition into the namespace.
According to the error message:
message_window* get_message_window(string title,string msj,int s){
should be:
message_window* windows::get_message_window(string title,string msj,int s){