# Bundled C++ Code that uses opencv into a .dll - issues

**URL:** <https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938>\
**Category:** C++\
**Tags:** highgui, windows, build\
**Created:** [August 26, 2021, 3:41pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938 "2021-08-26T15:41:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flyers\_Research](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@Flyers\_Research](https://forum.opencv.org/u/Flyers_Research)\
**Post date:** [August 26, 2021, 3:41pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/1 "2021-08-26T15:41:32Z")

</div>

Hi all,

I bundled code into a .dll that uses opencv, when I run the function from the .dll - it works correctly including opencv functions except for one issue.

Imread returns a nullptr when it tries to load an image.

When I take the code out of the .dll and create a console application that uses it, imread works perfectly. It is not a file path issue, and I want to reiterate that other opencv functiosn work in the .dll.

Thanks

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 26, 2021, 3:44pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/2 "2021-08-26T15:44:26Z")

</div>

> [@Flyers\_Research](#):
>
> Imread returns a nullptr when it tries to load an image.

it does NOT return a pointer in the 1st place. (but an empty Mat on failure)

can you please _show_, what you are doing here ?

---

<div class="post-metadata">

**Author:** ![Flyers\_Research](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@Flyers\_Research](https://forum.opencv.org/u/Flyers_Research)\
**Post date:** [August 26, 2021, 3:50pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/3 "2021-08-26T15:50:42Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d8480943a609c97bf60943a4fd30cdbca7e212db.png)

---

<div class="post-metadata">

**Author:** ![Flyers\_Research](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@Flyers\_Research](https://forum.opencv.org/u/Flyers_Research)\
**Post date:** [August 26, 2021, 3:52pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/4 "2021-08-26T15:52:12Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/304a08141e139c2ce9a69e4b4207a2902758aaa7.png)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 26, 2021, 3:57pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/5 "2021-08-26T15:57:57Z")

</div>

thanks for the info, but please NEVER post IMAGES of code or errors anywhere, but TEXT , (how do yo expect ppl to try, what you do ?), thank you.

---

<div class="post-metadata">

**Author:** ![Flyers\_Research](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@Flyers\_Research](https://forum.opencv.org/u/Flyers_Research)\
**Post date:** [August 26, 2021, 4:00pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/6 "2021-08-26T16:00:26Z")

</div>

Sorry, first post. Will try again.

---

<div class="post-metadata">

**Author:** ![Flyers\_Research](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@Flyers\_Research](https://forum.opencv.org/u/Flyers_Research)\
**Post date:** [August 26, 2021, 4:00pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/7 "2021-08-26T16:00:59Z")

</div>

```auto
#include "pch.h"
#include "framework.h"
#include "scratchdll.h"
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <vector>
#include <string>
#include <iostream>
#include <experimental/filesystem>

using namespace cv;
using std::experimental::filesystem::exists;

extern "C"
{
    _declspec(dllexport) void imshowgui() {
        String title = "C://Users//remove//Desktop//gc.png";
        std::cout << exists(title);
        std::cout << "\n" << title << "\n";
        Mat img = imread(title,IMREAD_UNCHANGED);
        
        std::cout << "\n" << img.rows << "," << img.cols << "\n";
        int c = waitKey(0);
        int i = 1;
        namedWindow("img", WINDOW_AUTOSIZE);
        while (i == 1) {
            imshow("img", img);
            c = waitKey(0);
            std::cout << waitKey << "\n";
            if (c == 99) {
                i = 0;
            }
        }
        std::cout << "Hello World!\n";
    }
}

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 26, 2021, 4:13pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/8 "2021-08-26T16:13:13Z")

</div>

well, that _should_ work…

- do yo see the expected console output ?

- do you see the GUI window ?

- can you attach the image (is it really a png ?) ?

- note, that `waitKey(0)` will wait forever,  
and that the GUI window needs to have focus here !

---

<div class="post-metadata">

**Author:** ![Flyers\_Research](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@Flyers\_Research](https://forum.opencv.org/u/Flyers_Research)\
**Post date:** [September 2, 2021, 3:38pm UTC](https://forum.opencv.org/t/bundled-c-code-that-uses-opencv-into-a-dll-issues/4938/9 "2021-09-02T15:38:33Z")

</div>

Hi berak,

Sorry for the late reply. The code creates a GUI window and attempts to load the PNG. The issue is that it returns an empty Mat.

Thanks
