Get Monitor Metadata from Windows - c

I need to be able to read metadata from the monitor, like: gamma, and monitor name, and hopefully, monitor size. Will I have to probe the registry? (SetupAPI???)
I tried DXGI (IDXGIOutput::GetDesc) and WinAPI (EnumDisplayDevicesA).
BROKEN:
HRESULT hr = IDXGIOutput1_GetDesc(output, &monitor_desc);
if(FAILED(hr)) {
assert(0);
}
printf("monitor name: %s\n", monitor_desc.Description);
ALSO BROKEN:
DISPLAY_DEVICE display_device_desc = { sizeof display_device_desc };
EnumDisplayDevices(NULL, 0, &display_device_desc, 0);
EnumDisplayDevices(display_device_desc.DeviceName,0,&display_device_desc, 0);
printf("monitor name: %s\n", display_device_desc.DeviceString);
I get Generic PnP Monitor instead of the correct name, Hannspree HF225.

1.This EnumDisplayDevices method works.
#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
int deviceIndex = 0;
while (EnumDisplayDevices(0, deviceIndex, &dd, 0))
{
std::wstring deviceName = dd.DeviceName;
int monitorIndex = 0;
while (EnumDisplayDevices(deviceName.c_str(), monitorIndex, &dd, 0))
{
std::wcout << dd.DeviceName << L", " << dd.DeviceString << L"\n";
++monitorIndex;
}
++deviceIndex;
}
return 0;
}
And another WMI method.
3...

Related

Is there a multiplex limit for perf_event.h event groups on Pi 3

I am am trying to run some analytics on PMU hardware/cache events on my Pi using perf_event.h. I am getting an error whenever I am attempting to add more than 7 events to an event group. There are several things I don't understand.
The main questions are as follows:
Is there a limit to the number of events that can be multiplexed on the Pi (ARM cortex A53 I believe)? Like I said I am failing at 8 and that seems low.
When multiplexing events, does perf_event.h detect the number of PMU counters and utilize all of them?
If there are limits on number of events in event groups, can I access this information using perf_event.h capabilities?
Do you know of any good resources to help me better understand the functionality of perf_event.h?
I am going to include the entire file I am attempting to run because I am new to this and I am not sure which part will be significant. The macros N and M at the top of the code are the number of hw events and hw cache events to use respectively. The error occurs whenever N+M > 7
I believe this code will run on any Linux system (stress package is used apt-get install stress)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <linux/hw_breakpoint.h>
#include <asm/unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#define N 0 // number of hw events to monitor
#define M 8 // number of hw events to monitor
int global_sigchld_trip = 0; // catch end child
void sighandler(int);
// function to add perf event to event list
// based on example from:
// http://web.eece.maine.edu/~vweaver/projects/perf_events/perf_event_open.html
static long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event,
pid, cpu, group_fd, flags);
return ret;
}
// no function for seeing if we can monitor kernel events
int no_function(int seconds)
{
sleep(seconds);
printf("\n\n Start stress \n\n");
system("stress -c 4 -t 10");
return 0;
}
int
main(int argc, char **argv){
signal(SIGCHLD, sighandler);
int num_hw_events = N;
uint pe_hw[7] = {
PERF_COUNT_HW_CPU_CYCLES,
PERF_COUNT_HW_INSTRUCTIONS,
PERF_COUNT_HW_CACHE_REFERENCES,
PERF_COUNT_HW_CACHE_MISSES,
PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
PERF_COUNT_HW_BRANCH_MISSES,
PERF_COUNT_HW_BUS_CYCLES,
};
char hw_name_arr[7][50] = {
"PERF_COUNT_HW_CPU_CYCLES",
"PERF_COUNT_HW_INSTRUCTIONS",
"PERF_COUNT_HW_CACHE_REFERENCES",
"PERF_COUNT_HW_CACHE_MISSES",
"PERF_COUNT_HW_BRANCH_INSTRUCTIONS",
"PERF_COUNT_HW_BRANCH_MISSES",
"PERF_COUNT_HW_BUS_CYCLES",
};
// cache events
int num_hw_cache_events = M;
uint pe_hw_cache[12] = {
(PERF_COUNT_HW_CACHE_L1D) | (PERF_COUNT_HW_CACHE_OP_READ << 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), //
(PERF_COUNT_HW_CACHE_L1I) | (PERF_COUNT_HW_CACHE_OP_READ << 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
(PERF_COUNT_HW_CACHE_LL) | (PERF_COUNT_HW_CACHE_OP_READ << 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
(PERF_COUNT_HW_CACHE_BPU) | (PERF_COUNT_HW_CACHE_OP_READ << 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
(PERF_COUNT_HW_CACHE_L1D) | (PERF_COUNT_HW_CACHE_OP_WRITE<< 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), //
(PERF_COUNT_HW_CACHE_LL) | (PERF_COUNT_HW_CACHE_OP_WRITE<< 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
(PERF_COUNT_HW_CACHE_BPU) | (PERF_COUNT_HW_CACHE_OP_WRITE<< 8) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
};
char hw_cache_name_arr[12][50] = {
"PERF_COUNT_HW_CACHE_L1D_read_miss",
"PERF_COUNT_HW_CACHE_L1I_read_miss",
"PERF_COUNT_HW_CACHE_LL_read_miss",
"PERF_COUNT_HW_CACHE_BPU_read_miss",
"PERF_COUNT_HW_CACHE_L1D_write_miss",
"PERF_COUNT_HW_CACHE_LL_write_miss",
"PERF_COUNT_HW_CACHE_BPU_write_miss",
};
struct perf_event_attr pat_arr[num_hw_events+num_hw_cache_events];
long long counts[num_hw_events+num_hw_cache_events];
int fd_arr[num_hw_events+num_hw_cache_events];
// initialize hw events
for(int i=0; i<num_hw_events; i++)
{
memset(&pat_arr[i], 0, sizeof(struct perf_event_attr));
pat_arr[i].type = PERF_TYPE_HARDWARE;
pat_arr[i].size = sizeof(struct perf_event_attr);
pat_arr[i].config = pe_hw[i];
if(i==0){pat_arr[i].disabled = 1;}
else{pat_arr[i].disabled = 0;}
pat_arr[i].exclude_kernel = 1;
pat_arr[i].exclude_hv = 1;
pat_arr[i].inherit = 1;
if(i==0){fd_arr[i] = perf_event_open(&pat_arr[i],0,-1,-1,0);}
else{fd_arr[i] = perf_event_open(&pat_arr[i],0,-1,fd_arr[0],0);}
if (fd_arr[i] == -1){
fprintf(stderr, "Error opening leader %llx\n", pat_arr[i].config);
exit(EXIT_FAILURE);
}
printf("FD%d: %d \t ITEM: %s\n",i,fd_arr[i], hw_name_arr[i]);
}
// initialize hw cache events
for(int i=0; i<num_hw_cache_events; i++)
{
memset(&pat_arr[i+num_hw_events], 0, sizeof(struct perf_event_attr));
pat_arr[i+num_hw_events].type = PERF_TYPE_HW_CACHE;
pat_arr[i+num_hw_events].size = sizeof(struct perf_event_attr);
pat_arr[i+num_hw_events].config = pe_hw_cache[i];
if(i+num_hw_events==0){printf("dis=1");pat_arr[i+num_hw_events].disabled = 1;}
else{printf("dis=0");pat_arr[i+num_hw_events].disabled = 0;}
pat_arr[i+num_hw_events].exclude_kernel = 1;
pat_arr[i+num_hw_events].exclude_hv = 1;
pat_arr[i+num_hw_events].inherit = 1;
if(i+num_hw_events==0){fd_arr[i+num_hw_events] = perf_event_open(&pat_arr[i+num_hw_events],0,-1,-1,0);}
else{fd_arr[i+num_hw_events] = perf_event_open(&pat_arr[i+num_hw_events],0,-1,fd_arr[0],0);}
if (fd_arr[i+num_hw_events] == -1){
printf("\ni: %d\nnhe:%d\n",i,num_hw_events);
fprintf(stderr, "Error opening leader %llx\n", pat_arr[i+num_hw_events].config);
exit(EXIT_FAILURE);
}
printf("FD%d: %d \t ITEM: %s\n",i+num_hw_events,fd_arr[i+num_hw_events], hw_cache_name_arr[i]);
}
// reset and enable counters
for(int i=0; i<num_hw_events+num_hw_cache_events; i++){
ioctl(fd_arr[i], PERF_EVENT_IOC_RESET,0);
ioctl(fd_arr[i], PERF_EVENT_IOC_ENABLE,0);
}
/////////////////// ACTION ///////////////////////////
/*-------- CHILD PROCESS BEING REDORDED -------*/
printf("\nSHOULD FORK RIGHTE HERE\n");
pid_t proc = fork();
if(proc==0){
printf("entered child process\n");
int no_sleep = 3;
no_function(no_sleep);
//x = silly_events(loop);
printf("exiting child process\n");
return 0;
}
/*-------- ACTION TAKEN DURRING REDORDING-------*/
else{
while(!global_sigchld_trip){
sleep(1);
for(int i=0;i<num_hw_events+num_hw_cache_events;i++){read(fd_arr[i], &counts[i], sizeof(long long));}
for(int i=0;i<num_hw_events+num_hw_cache_events;i++){ioctl(fd_arr[i], PERF_EVENT_IOC_RESET,0);}
for(int i=0;i<num_hw_events;i++){printf("%lld %s\t\n", counts[i], hw_name_arr[i]);}
printf("--------------------------------------\n");
for(int i=0;i<num_hw_cache_events;i++){printf("%lld %s\t\n", counts[i+num_hw_events], hw_cache_name_arr[i]);}
printf("\n\n");
}
}
for(int i=0;i<num_hw_events+num_hw_cache_events;i++){ioctl(fd_arr[i], PERF_EVENT_IOC_DISABLE,0);}
for(int i=0;i<num_hw_events+num_hw_cache_events;i++){read(fd_arr[i], &counts[i], sizeof(long long));}
for(int i=0;i<num_hw_events;i++){printf("Used %lld %s\t", counts[i], hw_name_arr[i]);}
for(int i=0;i<num_hw_cache_events;i++){printf("Used %lld %s\t", counts[i+num_hw_events], hw_cache_name_arr[i]);}
for(int i=0;i<num_hw_events;i++){close(fd_arr[i]);}
return 0;
}
void sighandler(int signum) {
printf("Caught signal %d, coming out...\n", signum);
global_sigchld_trip = 1;
}

How to get EventEnqueuedUtcTime from iot hub message using microsoft azure client sdk c

I have created the following c program based on a provided sample in order just to get messages from iot hub :
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include "azure_c_shared_utility/shared_util_options.h"
#include "iothub_client.h"
#include "iothub_message.h"
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/platform.h"
#include "iothubtransportmqtt.h"
#include "iothub_client_options.h"
#include "iothub_device_client_ll.h"
#include "iothub_device_client.h"
#include "iothub_client_sample_mqtt_esp8266.h"
/*String containing Hostname, Device Id & Device Key in the format: */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
static const char* connectionString = "HostName=mydevice";
static int callbackCounter;
//static char msgText[1024];
//static char propText[1024];
static bool g_continueRunning;
#define MESSAGE_COUNT 50
#define SET_TRUSTED_CERT_IN_SAMPLES
#define DOWORK_LOOP_NUM 3
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
#include "certs.h"
#endif
typedef struct EVENT_INSTANCE_TAG
{
IOTHUB_MESSAGE_HANDLE messageHandle;
size_t messageTrackingId; // For tracking the messages within the user callback.
} EVENT_INSTANCE;
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
int* counter = (int*)userContextCallback;
const char* buffer;
size_t size;
MAP_HANDLE mapProperties;
const char* messageId;
const char* correlationId;
// Message properties
if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
{
messageId = "<null>";
}
if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
{
correlationId = "<null>";
}
// Message content
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
{
(void)printf("unable to retrieve the message data\r\n");
}
else
{
(void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size);
// If we receive the work 'quit' then we stop running
if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
{
g_continueRunning = false;
}
}
// Retrieve properties from the message
mapProperties = IoTHubMessage_Properties(message);
if (mapProperties != NULL)
{
const char*const* keys;
const char*const* values;
size_t propertyCount = 0;
if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
{
if (propertyCount > 0)
{
size_t index;
std::string uid;
printf(" Message Properties:\r\n");
for (index = 0; index < propertyCount; index++)
{
(void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
if (std::strcmp(keys[index],"uid") == 0 )
{
uid = values[index];
std::cout << "uid is " << uid << std::endl;
}
}
(void)printf("\r\n");
}
}
}
/* Some device specific action code goes here... */
(*counter)++;
return IOTHUBMESSAGE_ACCEPTED;
}
// static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
// {
// EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
// size_t id = eventInstance->messageTrackingId;
// (void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, (int)id, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
// /* Some device specific action code goes here... */
// callbackCounter++;
// IoTHubMessage_Destroy(eventInstance->messageHandle);
// }
static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context)
{
(void)reason;
(void)user_context;
// This sample DOES NOT take into consideration network outages.
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
{
(void)printf("The device client is connected to iothub\r\n");
}
else
{
(void)printf("The device client has been disconnected\r\n");
}
}
void iothub_client_sample_mqtt_esp8266_run(void)
{
IOTHUB_CLIENT_HANDLE iotHubClientHandle;
g_continueRunning = true;
srand((unsigned int)time(NULL));
callbackCounter = 0;
int receiveContext = 0;
if (platform_init() != 0)
{
(void)printf("Failed to initialize the platform.\r\n");
}
else
{
if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL)
{
(void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
}
else
{
bool traceOn = true;
IoTHubClient_SetOption(iotHubClientHandle, OPTION_LOG_TRACE, &traceOn);
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
// For mbed add the certificate information
if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
{
(void)printf("failure to set option \"TrustedCerts\"\r\n");
}
#endif
(void)IoTHubDeviceClient_SetConnectionStatusCallback(iotHubClientHandle, connection_status_callback, NULL);
/* Setting Message call back, so we can receive Commands. */
if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
}
else
{
(void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
while (g_continueRunning)
{
ThreadAPI_Sleep(1000);
}
}
IoTHubClient_Destroy(iotHubClientHandle);
}
platform_deinit();
}
}
int main(void)
{
iothub_client_sample_mqtt_esp8266_run();
return 0;
}
Everything works fine. I get both messageid and correlationid correctly.
However I'd like to receive EventEnqueuedUtcTime at my callback which is mentioned here https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-define-inputs#configure-an-iot-hub-as-a-data-stream-input.
Since I'm quite new at this SDK I haven't a way to achieve this. Does anyone has an idea ?
I think you are confusing the device SDK and the service SDK. The EventEnqueuedUtcTime is available to a service side application that is reading the telemetry sent by devices to the IoT hub. I'm assuming that you expected to get that same value for a message delivered to your device via a Cloud to Device message which would be delivered to your callback function ReceiveMessageCallback. That property is not added to messages from the cloud to the device only messages from the device to the cloud.

How to use LLVM-C to JIT-compile the floating-point identity function

So far I have written the following code:
#include <stdlib.h>
#include <stdio.h>
#include <llvm-c/Core.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/Target.h>
#include <llvm-c/Transforms/Scalar.h>
int main (int argc, char const *argv[])
{
char *error = NULL;
LLVMLinkInMCJIT();
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();
LLVMModuleRef mod = LLVMModuleCreateWithName("minimal_module");
LLVMTypeRef identity_args[] = { LLVMDoubleType(), NULL };
LLVMValueRef identity = LLVMAddFunction(mod, "identity", LLVMFunctionType(LLVMDoubleType(), identity_args, 1, 0));
LLVMSetFunctionCallConv(identity, LLVMCCallConv);
LLVMValueRef n = LLVMGetParam(identity, 0);
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(identity, "entry");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, entry);
LLVMBuildRet(builder, n);
LLVMVerifyModule(mod, LLVMAbortProcessAction, &error);
LLVMDisposeMessage(error);
LLVMExecutionEngineRef engine;
error = NULL;
if(LLVMCreateJITCompilerForModule(&engine, mod, 2, &error) != 0) {
fprintf(stderr, "%s\n", error);
LLVMDisposeMessage(error);
abort();
}
LLVMDumpModule(mod);
LLVMGenericValueRef exec_args[] = {LLVMCreateGenericValueOfFloat(LLVMDoubleType(), 1.25)};
LLVMGenericValueRef exec_res = LLVMRunFunction(engine, identity, 1, exec_args);
fprintf(stderr, "\n");
fprintf(stderr, "; Running identity(%f) with JIT...\n", 1.25);
fprintf(stderr, "; Result: %f\n", LLVMGenericValueToFloat(LLVMDoubleType(), exec_res));
LLVMRemoveModule(engine, mod, &mod, &error);
LLVMDisposeModule(mod);
LLVMDisposeExecutionEngine(engine);
LLVMDisposeBuilder(builder);
return 0;
}
The corresponding implementation using 32-bit integers works, i.e. identity(42) will return 42. However the floating-point version above returns 0.0. Above program generates the following output:
; ModuleID = 'minimal_module'
source_filename = "minimal_module"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
define double #identity(double) {
entry:
ret double %0
}
; Running identity(1.250000) with JIT...
; Result: 0.000000
I am using LLVM-3.9 under Debian Jessie on an AMD computer. My question is: What am I doing wrong?
I found a solution to the problem.
Instead of using LLVMRunFunction one can use LLVMGetFunctionAddress to get the address of the function and call it directly using C (or using an FFI library).
#include <stdlib.h>
#include <stdio.h>
#include <llvm-c/Core.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/Target.h>
#include <llvm-c/Transforms/Scalar.h>
int main (int argc, char const *argv[])
{
char *error = NULL;
LLVMLinkInMCJIT();
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();
LLVMModuleRef mod = LLVMModuleCreateWithName("minimal_module");
LLVMTypeRef identity_args[] = { LLVMInt32Type() };
LLVMValueRef identity = LLVMAddFunction(mod, "identity", LLVMFunctionType(LLVMInt32Type(), identity_args, 1, 0));
LLVMSetFunctionCallConv(identity, LLVMCCallConv);
LLVMValueRef n = LLVMGetParam(identity, 0);
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(identity, "entry");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, entry);
LLVMBuildRet(builder, n);
LLVMVerifyModule(mod, LLVMAbortProcessAction, &error);
LLVMDisposeMessage(error);
LLVMExecutionEngineRef engine;
error = NULL;
if(LLVMCreateJITCompilerForModule(&engine, mod, 2, &error) != 0) {
fprintf(stderr, "%s\n", error);
LLVMDisposeMessage(error);
abort();
}
int (*fun)(int) = (int (*)(int))LLVMGetFunctionAddress(engine, "identity");
LLVMDumpModule(mod);
fprintf(stderr, "\n");
fprintf(stderr, "; Running identity(42) with JIT...\n");
fprintf(stderr, "; Result: %d\n", (*fun)(42));
LLVMRemoveModule(engine, mod, &mod, &error);
LLVMDisposeModule(mod);
LLVMDisposeExecutionEngine(engine);
LLVMDisposeBuilder(builder);
return 0;
}

Reading from raspberry pi

I am attempting to read gyroscopic data using a raspberry pi and am having a lot of problems. I think I understand a lot of the code and it is written properly.
The problem comes in when printing it onto raspberry screen, all I get is constant numbers that don't change at all.
Everything is based off of http://ozzmaker.com/berryimu/ I know the code is provided but I wanted to learn and possibly optimize it for my purposes. I am using https://www.adafruit.com/datasheets/L3GD20H.pdf gyroscope.
The program to read values is:
#include </home/pi/gyroSetup.c>
#include <stdio.h>
int main() {
setup();
int i;
int G[3];
for(i = 0;i<=1000;i++){
readGyro(G);
printf("%d ", G[0]);
printf("%d ", G[1]);
printf("%d\n", G[2]);
}
}
The gyroSetup.c is here:
#include <stdio.h>
#include <stdint.h>
#include "fcntl.h"
#include "linux/i2c-dev.h"
#define Gyr_adress 0x6b
#define Gyr_ctrl1 0b1110100
#define Gyr_ctrl4 0b00000000
#define Gyr_odr 0b00000000
int file;
void readBlock(uint8_t command, uint8_t size, uint8_t *data) {
int result = i2c_smbus_read_i2c_block_data(file, command, size, data);
if(result != size) {
printf("Failed to read block from i2c!");
{
}
void readGyro(int *T) {
uint8_t block[6];
if(ioctl(file,I2C_SLAVE, Gyr_adress) < 0) {
printf("failed to select device!");
}
readBlock(0x80 | 0x28, sizeof(block), block);
*T = (int16_t)(block[0] | block[1] << 8);
*(T+1) = (int16_t)(block[2] | block[3] << 8);
*(T+2) = (int16_t)(block[4] | block[4] << 8);
}
void writeGyro(uint8_t location, uint8_t value, int file) {
if(ioctl(file, I2C_SLAVE, Gyr_adress) < 0) {
printf("Failed to select i2c device");
}
int result = i2c_smbus_write_byte_data(file, location, value);
if(result == -1) {
printf("Failed to write byte!");
}
}
void setup(){
__u16 block[I2C_SMBUS_BLOCK_MAX];
file = open("/dev/i2c-1", O_RDWR);
if( file < 0) {
printf("Unable to open i2c bus!");
}
writeGyro(0x20, Gyr_ctrl1, file);
writeGyro(0x23, Gyr_ctrl4, file);
writeGyro(0x39, Gyr_odr, file);
}
I have no idea why when reading the values don't change so please help. When compiling there are no errors.

Finding address range of process ( C / Windows )

I am trying to inspect Notepad's memory in real time, so I can detect when a specific string is written there.
I guess step 1 would be to find out what is the memory range of that process.
This is what I have until now :
int main()
{
MEMORY_BASIC_INFORMATION meminfo;
unsigned char *addr = 0;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,7280);
VirtualQueryEx(hProcess,addr,&meminfo,sizeof(meminfo));
CloseHandle(hProcess);
}
I suppose I need to manipulate meminfo in some way to grab the information from there.
I need to find the full address range so I can iterate trough the addresses and search for the string.
Any help will be much appreciated, I apologize in advance if this is a stupid question but I'm just starting out with C.
Thanks
To get a list of all allocated, valid memory regions you can use VirtualQueryEx in a while loop like so:
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>
#include <tchar.h>
DWORD GetProcId(const TCHAR* procName)
{
DWORD procId = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
if (Process32First(hSnap, &procEntry))
{
do
{
if (!_tcsicmp(procEntry.szExeFile, procName))
{
procId = procEntry.th32ProcessID;
break;
}
} while (Process32Next(hSnap, &procEntry));
}
}
CloseHandle(hSnap);
return procId;
}
int main()
{
DWORD procid = GetProcId("ac_client.exe");
MEMORY_BASIC_INFORMATION meminfo;
unsigned char* addr = 0;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
MEMORY_BASIC_INFORMATION mbi;
while (VirtualQueryEx(hProc, addr, &mbi, sizeof(mbi)))
{
if (mbi.State != MEM_COMMIT || mbi.Protect == PAGE_NOACCESS)
{
std::cout << "base : 0x" << std::hex << mbi.BaseAddress << " end : 0x" << std::hex << (uintptr_t)mbi.BaseAddress + mbi.RegionSize << "\n";
}
addr += mbi.RegionSize;
}
CloseHandle(hProc);
}

Resources