Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash when accessing static variable exported with a def file

I am using a def file to export some static functions and variables from a dll. When accessing the static variable after importing the dll the program crashes. Any ideas why this is happening? I am using VS2017, Windows SDK 10.0.17763.0.

library.h

struct DLLEXPORT A {
  static int a;
  static int get();
};

struct B {
  static int b;
  static int get();
};

library.cpp

int A::a = 0; 
int A::get() {return a;}

int B::b = 0;
int B::get() {return b;}

library.def

LIBRARY

EXPORTS
  ?b@B@@2HA
  ?get@B@@SAHXZ

main.cpp

int main() {
  int a = A::get(); // Works fine
  int b = B::get(); // Works fine

  A::a = 1; // Works fine
  B::b = 1; // CRASH (Access violation writing location ...)
  return 0;
}
like image 388
tdd Avatar asked Oct 16 '22 07:10

tdd


1 Answers

I think def file entry lacks DATA attribute so B::b is treated as code which is typically-read only:

?b@B@@2HA DATA
like image 171
user7860670 Avatar answered Dec 19 '22 12:12

user7860670