win-dynamic-linking.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (C) 2015 Francesc Alted
  3. http://blosc.org
  4. License: MIT (see LICENSE.txt)
  5. Example program demonstrating use of the Blosc filter using the Windows Run-Time Dynamic Linking technique:
  6. https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx
  7. This allows to link your app in run-time with DLLs made with different compatible compilers
  8. (e.g. VS2013 and mingw-w64).
  9. To compile this program (be aware that you should match your compiler 32-bit/64-bit with your DLL):
  10. cl /Ox /Fewin-dynamic-linking.exe /I..\blosc win-dynamic-linking.c
  11. To run:
  12. $ win-dynamic-linking.exe
  13. Blosc version info: 1.7.0.dev
  14. Compression: 400000000 -> 19928862 (20.1x)
  15. Decompression succesful!
  16. Succesful roundtrip!
  17. */
  18. #include <stdio.h>
  19. #include <blosc.h>
  20. #include <windows.h>
  21. #define SIZE 100*1000*1000
  22. #define SHAPE {100,1000,1000}
  23. #define CHUNKSHAPE {1,1000,1000}
  24. /* Definition for the compression and decompression blosc routines */
  25. typedef int (__cdecl *COMPRESS_CTX)(int clevel, int doshuffle, size_t typesize,
  26. size_t nbytes, const void* src, void* dest,
  27. size_t destsize, const char* compressor,
  28. size_t blocksize, int numinternalthreads);
  29. typedef int (__cdecl *DECOMPRESS_CTX)(const void *src, void *dest,
  30. size_t destsize, int numinternalthreads);
  31. typedef char* (__cdecl *GET_VERSION_STRING)(void);
  32. int main(){
  33. HINSTANCE BDLL; /* Handle to DLL */
  34. COMPRESS_CTX blosc_compress_ctx; /* Function pointer for compression */
  35. DECOMPRESS_CTX blosc_decompress_ctx; /* Function pointer for decompression */
  36. GET_VERSION_STRING blosc_get_version_string;
  37. static float data[SIZE];
  38. static float data_out[SIZE];
  39. static float data_dest[SIZE];
  40. int isize = SIZE*sizeof(float), osize = SIZE*sizeof(float);
  41. int dsize = SIZE*sizeof(float), csize;
  42. int i;
  43. BDLL = LoadLibrary(TEXT("myblosc.dll"));
  44. if (BDLL == NULL) {
  45. printf("Cannot find myblosc.dll library!\n");
  46. goto out;
  47. }
  48. blosc_compress_ctx = (COMPRESS_CTX)GetProcAddress(BDLL, "blosc_compress_ctx");
  49. if (!blosc_compress_ctx) {
  50. // handle the error
  51. printf("Cannot find blosc_compress_ctx() function!\n");
  52. goto out;
  53. }
  54. blosc_decompress_ctx = (DECOMPRESS_CTX)GetProcAddress(BDLL, "blosc_decompress_ctx");
  55. if (!blosc_decompress_ctx) {
  56. // handle the error
  57. printf("Cannot find blosc_decompress_ctx() function!\n");
  58. goto out;
  59. }
  60. blosc_get_version_string = (GET_VERSION_STRING)GetProcAddress(BDLL, "blosc_get_version_string");
  61. if (!blosc_get_version_string) {
  62. // handle the error
  63. printf("Cannot find blosc_get_version_string() function!\n");
  64. goto out;
  65. }
  66. for(i=0; i<SIZE; i++){
  67. data[i] = i;
  68. }
  69. /* Register the filter with the library */
  70. printf("Blosc version info: %s\n", blosc_get_version_string());
  71. /* Compress with clevel=3, shuffle active, 16-bytes data size, blosclz and 2 threads */
  72. csize = blosc_compress_ctx(3, 1, 16, isize, data, data_out, osize, "blosclz", 0, 2);
  73. if (csize == 0) {
  74. printf("Buffer is uncompressible. Giving up.\n");
  75. return 1;
  76. }
  77. else if (csize < 0) {
  78. printf("Compression error. Error code: %d\n", csize);
  79. return csize;
  80. }
  81. printf("Compression: %d -> %d (%.1fx)\n", isize, csize, (1.*isize) / csize);
  82. /* Decompress */
  83. dsize = blosc_decompress_ctx(data_out, data_dest, dsize, 1);
  84. if (dsize < 0) {
  85. printf("Decompression error. Error code: %d\n", dsize);
  86. return dsize;
  87. }
  88. printf("Decompression succesful!\n");
  89. for(i=0;i<SIZE;i++){
  90. if(data[i] != data_dest[i]) {
  91. printf("Decompressed data differs from original!\n");
  92. return -1;
  93. }
  94. }
  95. printf("Succesful roundtrip!\n");
  96. return 0;
  97. out:
  98. FreeLibrary(BDLL);
  99. return -1;
  100. }