What value is LVM_SCROLL assigned? - winforms

What value is LVM_SCROLL assigned?
i.e. SW_HIDE is assigned to 0
Public Const SW_HIDE = 0
What value is LVM_SCROLL assigned too?
Where can this value and other constant values be found?
I program in a language called PL/B. It gives access to execute windows APIs but it does not have all the constants defined. Of course, the examples on the Web use the constant name so I have to track down the value.
I normally found these values in a file called WIN32API.TXT. It has many constant's values defined but as I found out it does not have LVM_SCROLL defined.

PInvoke.Net will tell you:
public enum ListViewMessages : int
{
LVM_FIRST = 0x1000,
LVM_SCROLL = (LVM_FIRST + 20)
}

Try building a test application to print out the value of unknown constants:
#include <iostream>
#include <commctrl.h>
int main () {
std::cout << "LVM_SCROLL = " << LVM_SCROLL << "\n";
return 0;
}

Related

Vulkan vkCreateInstance - Access violation writing location 0x0000000000000000

I am trying to write a basic program using Vulkan, but I keep getting a runtime error.
Exception thrown at 0x00007FFDC27A8DBE (vulkan-1.dll) in VulkanTest.exe: 0xC0000005: Access violation writing location 0x0000000000000000.
This seems to be a relatively common issue, resulting from a failure to initialize the arguments of the vkCreateInstance function. I have tried all of the solutions I found proposed to others, even initializing things I am fairly certain I don't need to, and I still haven't been able to solve the problem. The program is written in C using the MSVC compiler.
#include "stdio.h"
#include "SDL.h"
#include "vulkan\vulkan.h"
#include "System.h"
int main(int argc, char *argv[])
{
//Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Error");
}
printf("Success");
//Initialize Vulkan
VkInstance VulkanInstance;
VkApplicationInfo VulkanApplicationInfo;
VulkanApplicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
VulkanApplicationInfo.pNext = 0;
VulkanApplicationInfo.pApplicationName = "VulkanTest";
VulkanApplicationInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
VulkanApplicationInfo.pEngineName = "VulkanTest";
VulkanApplicationInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
VulkanApplicationInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo VulkanCreateInfo = {0,0,0,0,0,0,0,0};
VulkanCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
VulkanCreateInfo.pNext = 0;
VulkanCreateInfo.pApplicationInfo = &VulkanApplicationInfo;
VulkanCreateInfo.enabledLayerCount = 1;
VulkanCreateInfo.ppEnabledLayerNames = "VK_LAYER_KHRONOS_validation";
vkEnumerateInstanceExtensionProperties(0, VulkanCreateInfo.enabledExtensionCount,
VulkanCreateInfo.ppEnabledExtensionNames);
//Create Vulkan Instance
if(vkCreateInstance(&VulkanCreateInfo, 0, &VulkanInstance) != VK_SUCCESS)
{
printf("Vulkan instance was not created");
}
//Create SDL Window
SDL_Window* window;
window = SDL_CreateWindow("VulkanTest", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_VULKAN || SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_Delay(10000);
return 0;
}
Are you sure the call to vkCreateInstance() is what is crashing? I have not tried to debug the code you have shown (that is your job), but just looking at the calls that the code is making, it should be the call to vkEnumerateInstanceExtensionProperties() that is crashing (if it even compiles at all!).
The 2nd parameter of vkEnumerateInstanceExtensionProperties() expects a uint32_t* pointer, but you are passing in a uint32_t value (VulkanCreateInfo.enabledExtensionCount) that has been initialized to 0. So, that would make the pPropertyCount parameter be a NULL pointer (if it even compiles).
You are passing VulkanCreateInfo.ppEnabledExtensionNames in the 3rd parameter (if that even compiles), and ppEnabledExtensionNames has been initialized to NULL. Per the documentation for vkEnumerateInstanceExtensionProperties():
If pProperties is NULL, then the number of extensions properties available is returned in pPropertyCount. Otherwise, pPropertyCount must point to a variable set by the user to the number of elements in the pProperties array, and on return the variable is overwritten with the number of structures actually written to pProperties.
Since pPropertCount is NULL, vkEnumerateInstanceExtensionProperties() has nowhere to write the property count to! That would certainly cause an Access Violation trying to write to address 0x0000000000000000.
The documentation clears states:
pPropertyCount must be a valid pointer to a uint32_t value
On top of that, your call to vkEnumerateInstanceExtensionProperties() is just logically wrong anyway, because the 3rd parameter expects a pointer to an array of VkExtensionProperties structs, but VulkanCreateInfo.ppEnabledExtensionNames is a pointer to an array of const char* UTF-8 strings instead.
In other words, you should not be using vkEnumerateInstanceExtensionProperties() to initialize criteria for the call to vkCreateInstance(). You are completely misusing vkEnumerateInstanceExtensionProperties(). You probably meant to use SDL_Vulkan_GetInstanceExtensions() instead, eg:
uint32_t ExtensionCount = 0;
if (!SDL_Vulkan_GetInstanceExtensions(NULL, &ExtensionCount, NULL))
{
...
}
const char **ExtensionNames = (const char **) SDL_malloc(sizeof(const char *) * ExtensionCount);
if (!ExtensionNames)
{
...
}
if (!SDL_Vulkan_GetInstanceExtensions(NULL, &ExtensionCount, ExtensionNames))
{
SDL_free(ExtensionNames);
...
}
VulkanCreateInfo.enabledExtensionCount = ExtensionCount;
VulkanCreateInfo.ppEnabledExtensionNames = ExtensionNames;
if (vkCreateInstance(&VulkanCreateInfo, 0, &VulkanInstance) != VK_SUCCESS)
{
...
}
SDL_free(ExtensionNames);
...

Value of multiple struct variables is same unintentionally

I have a code that does OOP like Java.
I have separated the interface and the implementation in separate files names demo.h and demo.c.
demo.h
#ifndef DEMO_H
#define DEMO_H
typedef struct {
/*
This is the variable that will be set by setter method
and its value will be extracted by getter method.
This variable must not be directly accessible by the programmer.
*/
int num;
void (* setNum)(int); // This function will set the value of variable "num".
int (* getNum)(); // This function will return the value of variable "num".
} *Demo; // As objects in java are always called by reference.
Demo newDemo(); // This function will create an instance of class(struct here) Demo and return.
/* This is equivalent to:
Demo obj = new Demo();
int java.
I want my users to create instance of this class(struct here) like this:
Demo obj = newDemo();
here in this code.
*/
#endif
And the implementation:
demo.c
#include <stdlib.h>
#include "demo.h"
Demo demo; /* I have created a global variable so that it is accessible
in setNum and getNum functions. */
void setNum(int num) {
demo->num = num; // This is where the global demo is accessed.
}
int getNum(Demo obj) {
return demo->num; // This is where the global demo is accessed.
}
Demo newDemo() {
Demo obj; // This will be the returned object.
obj = (Demo)malloc(sizeof(*obj)); /* Allocating separate memory to
obj each time this function is called. */
/* Setting the function pointer. */
obj->setNum = setNum;
obj->getNum = getNum;
/* As obj is at different memory location every time this function is called,
I am assigning that new location the the global demo variable. So that each variable
of the Demo class(struct here) must have a different object at different memory
location. */
demo = obj;
return obj; // Finally returning the object.
}
This is how I have implemented the main function:
main.c
#include "demo.h"
#include <stdio.h>
int main() {
void displayData(Demo);
Demo obj1 = newDemo();
Demo obj2 = newDemo();
Demo obj3 = newDemo();
obj1->setNum(5);
obj2->setNum(4);
obj3->setNum(12);
displayData(obj1);
displayData(obj2);
displayData(obj3);
return 0;
}
void displayData(Demo obj) {
int num = obj->getNum();
fprintf(stdout, "%d\n", num);
}
On compilation and execution on my mac book pro:
> gcc -c demo.c
> gcc main.c demo.o -o Demo
> ./Demo
The output is:
12
12
12
But the desired output is:
5
4
12
What am I doing wrong?
Please help.
I don't want my users to pass the struct pointer as an argument as:
Demo obj = newDemo();
obj->setName(obj, "Aditya R.Singh"); /* Creating the program this way was successful as my
header file had the declaration as:
typedef struct demo {
int num;
void (* setNum)(struct demo, int); // This is what I don't desire.
void (* getNum)(struct demo); // This is what I don't desire.
} *Demo;
I want to keep it like the way it is in my current
demo.h*/
/* I don't want to pass obj as an argument. All I want to do this is this way. */
obj->setName("Aditya R.Singh");
Is there any way possible to do this and get the desired output?
Please help, thanks!
I have absolutely no idea of c++, but in your code, I think, demo = obj; is the problem. demo is global, right? It will get overwritten with evety call to newDemo().
Side effect : Memory leak.

Passing values to macros by for loop

I want to pass values to the macro through for loop,but when i try to pass values it gives error, please help m as fast as possible. When values of i are passed to macro as Valve(i) it gives error
my code given below:
#define Valve(x) stTest.bValve##x##_Cmd
typedef struct OperationFlags
{
int bValve1_Cmd;
int bValve2_Cmd;
}FLAGS_TypeDef;
void main(void)
{
FLAGS_TypeDef stTest;
int j,i;
stTest.bValve1_Cmd = 4;
stTest.bValve2_Cmd = 9;
for(i=1;i<=2;i++)
{
j=Valve(1);
printf("%d",j);
}
}
It is normal!
The preprocessor (the "thing" that processes the macros) is run BEFORE the C compiler. So, it is only valid when it produces compilable code.
In your case, if you use the code you show
j=Valve(1)
it will work for that value, since it will produce:
j=stTest.bValve1_Cmd
but it will do the entire loop only with that value.
When you change the parameter "1" with the "i" for actually doing the loop, then it will produce:
j=stTest.bValvei_Cmd
which is invalid.
To do what you want, just use a vector:
typedef struct OperationFlags
{
int bValve_Cmd[2];
}FLAGS_TypeDef;
#define Valve(x) stTest.bValve_Cmd[x]
//....
for(i=1;i<=2;i++)
{
j=Valve(1);
printf("%d",j);
}
Macro replacement is done well before runtime, so you cannot use a variable X containing the value 2 to get stTest.bValve2_Cmd. Instead, you will get stTest.bValveX_Cmd, for which no symbol exists.
You will have to find another way of doing this, such as having an array of values for which you can use X to select:
#define Valve(x) stTest.bValveX_Cmd[x]
typedef struct OperationFlags {
int bValveX_Cmd[2];
} FLAGS_TypeDef;
try this #define Valve(x) (x == 1 ? stTest.bValve1_Cmd : stTest.bValve2_Cmd)
#define Valve(x) (*(&stTest.bValve1_Cmd + (x-1)))
note : It may not work if the environment changes. Also it can not be used in the bit field.
add check
#define Valve(x) (*(&stTest.bValve1_Cmd + (x-1))); \
assert(offsetof(FLAGS_TypeDef, bValve2_Cmd) == sizeof(int))

How do I not share an external variable across instances?

EDIT: this is a rewrite of the question since it was so unspecific before.
So I'm having a problem solving the issue of variables being shared across instances in C extensions. Here is an example of what I'm encountering.
>> t = SCOPE::TestClass.new #=> #<SCOPE::TestClass:0x000001011e86e0>
>> t.set = 4 #=> 4
>> t.get #=> 4
>> v = SCOPE::TestClass.new #=> #<SCOPE::TestClass:0x00000101412bf0>
>> v.set = 5 #=> 5
>> v.get #=> 5
>> t.get #=> 5
Would the best solution in the code below be to simply use ruby variables that you can set up like
void rb_define_variable(const char *name, VALUE *var)
Or is there a solution in C that I'm not seeing or understanding?
Code:
#include <stdlib.h>
#include <ruby.h>
VALUE TestClass;
VALUE SCOPE;
VALUE test_var;
VALUE set(self, val);
VALUE get();
VALUE set(VALUE self, VALUE val) {
test_var = NUM2INT(val);
return Qnil;
}
VALUE get() {
return INT2NUM(test_var);
}
void Init_scope()
{
SCOPE = rb_define_module("SCOPE");
TestClass = rb_define_class_under(SCOPE, "TestClass", rb_cObject);
rb_define_method(TestClass, "set=", set, 1);
rb_define_method(TestClass, "get", get, 0);
}
Ok, now I think I see the problem. Your
VALUE test_var;
is a sort of "shared" value among every instance of the test class. This is an error of course, since it is overwritten as new instances are created or method set is called. So you can have just a single instance and a value shared among every instance.
Of course you are doing something wrong: ruby has to provide context and a way to retrieve it, likely the proto for the get function must have at least VALUE self as argument, like set. The value can't be stored into global or static local variable: it must be someway stored into the "context" of the object. In order to know how to do so, I need a fast tutorial about ruby ext. In the meantime try to read deeper here.
In particular, focus your attention on "Accessing Variables" and how you define instance variables.
I have done this, that seems to work; you could like to work on it to achieve your extension purpose(s) (I've renamed something, and fixed something else; I've also removed the INT2NUM and NUM2INT stuff, you can put it back at your need)
#include <stdlib.h>
#include <ruby.h>
VALUE TestClass;
VALUE SCOPE;
VALUE set(VALUE, VALUE);
VALUE get(VALUE);
VALUE set(VALUE self, VALUE val) {
(void)rb_iv_set(self, "#val", val);
return Qnil;
}
VALUE get(VALUE self) {
return rb_iv_get(self, "#val");
}
void Init_RubyTest()
{
SCOPE = rb_define_module("RubyTest");
TestClass = rb_define_class_under(SCOPE, "TestClass", rb_cObject);
rb_define_method(TestClass, "set=", set, 1);
rb_define_method(TestClass, "get", get, 0);
}
This question can't be answered fully if we don't know how "C extension" (I suppose, to Ruby?) works, and sincerely I don't know.
A "global" variable declared static is local to the file where it is defined and can't be accessed from outside, i.e. it is global inside that file, but it is not a global for all the linked files.
func1 can access bar, indeed; it can't just because the symbol is not known until it is declared (for the same reason func1 can't call func2, or at least compiler gives a warning for the missing prototype, then the code to func2 will be found anyway), but anyway, once the symbol is known, it can be accessed. On the contrary, those variables bar and foo can't be seen from outside (and so are not global) since the symbols foo and bar are not visible.
If this code is supposed to be compiled as a shared object or a static library, foo and bar won't be visible by the code that links the shared object / static library.
Global variables are according to the specification of ruby c extensions shared amongst each other (see documentation). It is the best option to limit variable scopes to the least visible which does the job. If you happen to have a shared variable it should be at least safe to synchronization issues.

Can a macro be used for read-only access to a variable?

Can you define a macro that accesses a normal variable, but in a read-only fashion (other than defining it as a call to a function)? For example, can the VALUE macro in the following code be defined in such a way that the dostuff() function causes a compile error?
struct myobj {
int value;
}
/* This macro does not satisfy the read-only requirement */
#define VALUE(o) (o)->value
/* This macro uses a function, unfortunately */
int getvalue(struct myobj *o) { return o->value; }
#define VALUE(o) getvalue(o)
void dostuff(struct myobj *foo) {
printf("The value of foo is %d.\n", VALUE(foo)); /* OK */
VALUE(foo) = 1; /* We want a compile error here */
foo->value = 1; /* This is ok. */
}
Ok, I came up with one:
#define VALUE(o) (1 ? (o)->value : 0)
If the variable is always numeric, this works:
#define VALUE(x) (x+0)
or in the context of your example,
#define VALUE(x) (x->value+0)
See §6.5.17 in the C standard (C99 & C1x): “A comma operator does not yield an lvalue.”
#define VALUE(x) (0, x)
(Not portable to C++.)
Try
#define VALUE(o) (const int)((o)->value)
Is this a puzzle or is it an engineering task?
If it's an engineering task, then there are better ways to get opacity of structures in C. In this blog article, I wrote a decent enough description of how to do that in C.

Resources