acdbGetPreviewBitmapFromDwg throws System.AccessViolationException for some files

acdbGetPreviewBitmapFromDwg throws System.AccessViolationException for some files

muaslam
Enthusiast Enthusiast
368 Views
0 Replies
Message 1 of 1

acdbGetPreviewBitmapFromDwg throws System.AccessViolationException for some files

muaslam
Enthusiast
Enthusiast

Hi,

 

I have been facing an exception while trying to get Thumbnail from a DWG file. Most of the files work correctly, but some of the files do throw System.AccessViolationException on the call to method acdbGetPreviewBitmapFromDwg. 

 

I have tried to explore several options to handle this exception, but found no help on this.

 

 

bool ReadThumbnail(std::wstring filename, std::wstring thumbnailPath, RDwgError ^ error) 
{ 
    // acdbGetPreviewBitmapFromDwg depends on hostApp being initialized: 
    if (!s_hostAppInitialized && Acad::eOk == acdbSetHostApplicationServices(&RealDwg::RealDwgHostApp::Instance()))
        s_hostAppInitialized = true; 
    if (!s_hostAppInitialized) 
    { 
        error->Code = 2;
        error->Description = "Host App Not Initialized."; 
        return false; 
    } 
    HBITMAP hBitmap = NULL; 
    HPALETTE hPalette = NULL; 
    char* filenameCharPointer = new char[1024];
    wcstombs(filenameCharPointer, filename.c_str(), 1024); 
    if (!fopen(filenameCharPointer, "r")) 
    { 
        error->Description = "Source file does not exist"; 
        error->Code = 4;
        return false;
    } 
    int bitmapresult; 
    try 
    { 
        bitmapresult = acdbGetPreviewBitmapFromDwg(filename.c_str(), &hBitmap, &hPalette);
    } 
    catch (int exception) 
    { 
        std::cout << "Exception occurred : " << exception << std::endl; 
    } 
    if (!bitmapresult) 
    { 
        error->Description = "Could not get preview bitmap from dwg"; 
        error->Code = 3; 
        return false; 
    } 
    std::cout << "Storing file now" << std::endl; 
    CImage image; 
    image.Attach(hBitmap);
    image.Save(thumbnailPath.c_str(), Gdiplus::ImageFormatBMP); 
    std::cout << "File stored successfully" << std::endl; 
    DeleteObject(hBitmap); 
    DeleteObject(hPalette);
    return true; 
}

 

 

 

 

 

Any help this regards.

 

 

Regards,

Umar

0 Likes
369 Views
0 Replies
Replies (0)