Cannot get bitmap from native DLL from multi threaded Java app.

I have a native DLL that gives me a HBITMAP (Windows handle to bitmap). I have made a JNI wrapper for this that gives me a BufferedImage back. The native code simple gets the actual bitmap for the handle, converts the RGB byte triplets to RGB ints, puts this in a jintArray and creates a BufferedImage.
This all works fine within a single Java thread. If I do this multi threaded (thread 1 calls the native function and several seconds later, thread 2 calls the native function) I keep getting the last image of the first thread. Can enyone help me out?
I know that this is probably a question for MSDN but I just know that they will tell me its a JNI question.
The native code:
JNIEXPORT jobject JNICALL Java_somenamespace_getPicture
(JNIEnv *env, jclass cl, jstring serialNumber)
jobject jImage;
jclass jImageClass;
jmethodID jMethod;
jfieldID jField;
jintArray rgbArray;
char *str;
char *charArray;
int *intArray;
int w, h, index, type;
HBITMAP hbmp;
BITMAP bmp;
str = (char*)((*env)->GetStringUTFChars(env, serialNumber, NULL));
// Gets the HBITMAP handle from the native DLL. The first argument is to select the device, based on serial number.
getPicture(str, &hbmp);
(*env)->ReleaseStringUTFChars(env, serialNumber, str);
// Get the BITMAP for the HBITMAP
GetObject(hbmp, sizeof(BITMAP), &bmp);
// Create my BufferedImage
jImageClass = (*env)->FindClass(env, "java/awt/image/BufferedImage");
jMethod = (*env)->GetMethodID(env, jImageClass, "<init>", "(III)V");
jField = (*env)->GetStaticFieldID(env, jImageClass, "TYPE_INT_RGB", "I");
type = (*env)->GetStaticIntField(env, jImageClass, jField);
jImage = (*env)->NewObject(env, jImageClass, jMethod, bmp.bmWidth, bmp.bmHeight, type);
// Get the RGB byte triplets of the BITMAP
charArray = (char *)malloc(sizeof(char) * bmp.bmHeight * bmp.bmWidth * 3);
GetBitmapBits(hbmp, sizeof(char) * bmp.bmHeight * bmp.bmWidth * 3, charArray);
// Allocate space to store the RGB ints and convert the RGB byte triplets to RGB ints
intArray = (int *)malloc(sizeof(int) * bmp.bmHeight * bmp.bmWidth);
index = 0;
for (h = 0; h < bmp.bmHeight; ++h)
for (w = 0; w < bmp.bmWidth; ++w)
int b = *(charArray + index * 3) & 0xFF;
int g = *(charArray + index * 3 + 1) & 0xFF;
int r = *(charArray + index * 3 + 2) & 0xFF;
*(intArray + index) = 0xFF000000 | (r << 16) | (g << 8) | b;
++index;
// Create a jintArray for the C RGB ints
rgbArray = (*env)->NewIntArray(env, bmp.bmHeight * bmp.bmWidth);
(*env)->SetIntArrayRegion(env, rgbArray, 0, bmp.bmHeight * bmp.bmWidth, (jint*)intArray);
// Use BufferedImage.setRGB() to fill the image
jMethod = (*env)->GetMethodID(env, jImageClass, "setRGB", "(IIII[III)V");
(*env)->CallVoidMethod(env, jImage, jMethod,
0, // int startX
0, // int startY
bmp.bmWidth, // int width
bmp.bmHeight, // int height
rgbArray, // int[] rgbArray
0, // int offset
bmp.bmWidth); // int scansize
// Free stuff
(*env)->DeleteLocalRef(env, rgbArray);
free(charArray);
free(intArray);
return jImage;
}I already tried working with native HDCs (GetDC, CreateCompatibleBitmap, SelectObject, GetDIBits, ...) but this just complicates stuff and gives me the same result.

Have you verified what the "native DLL" gives you back on the second call?The HBITMAP handle returned is the same each call (no matter which thread). The actual BITMAP associated with this handle is only updated in the first thread.
How are you determining that the image is the same?If I point the camera (I don't get a stream, I just get individual images) to a moving scene, I get different images for each call from within the first thread. The second thread only gets the latest image from the first thread. There is no concurrency of the threads and all methods are synchronized.
Specifically did you verify that the problem is not that your test code itself is using the same instance?Yes, I tested the native side as well as the Java side. The BufferedImage is always an exact copy of the native BITMAP.
Try printing out the hash for each instance.I have written the image to a file in the native code itself to eliminate anything related to the mechanism of returning the BufferedImage. This had the same result.
The return values of all native calls all indicate successful calls.
I am suspecting the native side of creating a second graphical context for the second thread while it keeps refering to the first context.
(I will start a thread on the MSDN as well and will post here if anything turns up.)

Similar Messages

  • IPhone 3 I cannot get my pictures to download from iPhoto.  I keep getting the error message required file cannot be found.

    I cannot get my pictures to download from my iPhoto from an albumto the iPhone 3 (one of the kids phones). I was able to download an Event.  I have changed the name.  I have used my iPhone 4 and the pictures from the album downloaded just fine. I have looked at the message board and I have deleted the cache and done all the things that were suggested.  WHAT IS THE DEAL!! I have been working on my phone and the kids phones for about five hours now and super frustrated!!! 

    I used Terminal to change the permissions on the folder in question.  I followed the instructions in this article:
    iTunes: Missing folder or incorrect permissions may prevent authorization
    In my case, the folder was there, so I needed the command to change permissions on the folder, not to create one.   I was hesitant to use Terminal b/c I know that if I made an error I could wipe out my hard drive or render my computer unusable.  So to be SURE I didn't make an error, I carefully copied the command from that page and *pasted* it into Terminal.  Also, before I could do anything in Terminal, I had to go change my admin password (it had been a blank password before and that's not acceptable for making changes in Terminal).  I was just super careful when entering my password or doing anything else while Terminal was open (making sure I didn't accidently hit the spacebar or another key, etc.)  And it fixed the problem right away.
    What was confusing for me was that the iTunes error message said to change permissions in the FINDER, which is what I was trying to do.  It didn't mention Terminal.  What would really be helpful is if Apple included a link to a page like this in their error message.

  • My iPad was stolen in brazil.  i cannot get the manufacturer id number from my iPhone or my macbook air....they are all linked together though.  any suggestions on how and if i can possibly retrieve this and thus locate the device? thanks julie

    my iPad was stolen in brazil.  i cannot get the manufacturer id number from my iPhone or my macbook air....they are all linked together though.  any suggestions on how and if i can possibly retrieve this and thus locate the device? thanks julie

    Launch iTunes, go to the Preferences, select Devices, and hover your cursor over the backup file for the iPod. That should show you the serial number, IMEI, and possibly the MEID.
    The manufacturer ID will not help you locate the iPad, though. If you set up the Find My iPad service in the Settings prior to the iPad going missing, then there is a chance that can be tracked, though it depends on a number of factors (the iPad is turned on, connected to a WiFi or 3G network, and has not had the Find My iPad service disabled or the iPad been completely restored). 
    If you did not set up Find My iPad, then there is no way to track it. Contact the police and change any passwords for any online accounts you may have used from your iPad (the iTunes Store, for instance). 
    Regards.

  • I cannot get a CD to eject from my mac book pro

    I cannot get a CD to eject from my mac book pro. Help!

    CD/DVD - Eject Stuck
    CD/DVD - Eject Stuck (2)
    CD/DVD - Eject Stuck (3)
    CD/DVD - Eject Stuck (4)
    CD/DVD - Eject Stuck (5)

  • I cannot get any of my information from my Iphone or Ipad to sync to my Icloud account. Any Suggestions?

    I cannot get any of my information from my Iphone or Ipad to sync with my Icloud account. Any suggestions?

    Well, what exactly have you done so far?
    Is iCloud activated?
    What data is marked to be synced?
    What happens on icloud.com?
    Have you installed the iCloud utility on your computer?

  • I cannot get .arw or jpeg files from my Sony A6000 to be taken into my Lightroom 4.4 via the normal "synchronise" process.  I have updated my Pc to Adobe RAW/DNG 8.4 with no improvement.  Can you help please?

    I cannot get .arw or jpeg files from my Sony A6000 to be taken into my Lightroom 4.4 via the normal "synchronise" process.  I have updated my Pc to Adobe RAW/DNG 8.4 with no improvement.  Can you help please?

    With regards to your .arw files:
    You need to either purchase and install Lightroom 5.4 -- OR -- download and install and use the FREE Adobe DNG Converter version 8.4, did you actually launch and use the DNG Converter?
    There should be no problem importing JPG files into Lightroom from your camera, what happens when you try? Is there an error message?

  • Just bought the new i phone 4s, set up my apple id, verified with email. I can sign into it on the computer no problem but cannot get to it through iphone. Unable to download apps or music. let me know if anybody has any solutions, thanks

    Just bought the new i phone 4s, set up my apple id, verified with email. I can sign into it on the computer no problem but cannot get to it through iphone. Unable to download apps or music. let me know if anybody has any solutions, thanks

    If you have an old ID in Settings>iCloud as a result of updating to iOS 7, to change it you have to go to Settings>iCloud, tap Delete Account, provide the password for the old ID when prompted to turn off Find My iPhone, then sign back in with the ID you wish to use.  If you don't know the password for your old ID, or if it isn't accepted, go to https//appleid.apple.com, click Manage my Apple ID and sign in with your current iCloud ID.  Tap edit next to the primary email account, tap Edit, change it back to your old email address and save the change (you should need to verify the old account).  Then edit the name of the account to change it back to your old email address.  You can now use your current password to turn off Find My iPhone on your device, even though it prompts you for the password for your old account ID. Then go to Settings>iCloud, tap Delete Account and choose Delete from My iDevice when prompted (your iCloud data will still be in iCloud).  Next, go back to https//appleid.apple.com and change your primary email address and iCloud ID name back to the way it was.  Now you can go to Settings>iCloud and sign in with your current iCloud ID and password.

  • [E2010SP3] [EWS2.0] [C#][Windows] - Multi-threaded EWS App is encountering ArgumentOutOfRange - Count cannot be less then zero

    Hi,
           I have a multi-threaded EWS app that is pushing email, appointments, and contacts to Exchange2010SP3. Each thread is using a separate instance ExchangeService and is working with a different mailbox on each thread. We are seeing that
    when 4 or more threads are running we are starting to see ArgumentOutOfRange [Count cannot be less then zero] exceptions in the EWS library. These errors appear to be happening sporadically.
    Here is an example of one of stack traces:
    Stack Trace -- 
    at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count) 
    at System.IO.StreamWriter.Write(String value) 
    at System.IO.TextWriter.SyncTextWriter.Write(String value) 
    at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.BuildEwsHttpWebRequest() 
    at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) 
    at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalBindToItems(IEnumerable`1 itemIds, PropertySet propertySet, ServiceErrorHandling errorHandling) at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToItem[TItem](ItemId itemId, PropertySet
    propertySet) at Quest.Apollo.MigrationProviders.Interfaces.GenericRetryAction.InternalRetry[T](RetryMethod`1 retryMethod) in c:\Builds\272\Apollo\1.6.0 Nightly\Sources\Apollo.MigrationProviders.Interfaces\GenericRetryAction.cs:line 111 at Quest.Apollo.MigrationProviders.Interfaces.GenericRetryAction.Retry[T](RetryMethod`1
    retryMethod, Boolean disableRetry) in c:\Builds\272\Apollo\1.6.0 Nightly\Sources\Apollo.MigrationProviders.Interfaces\GenericRetryAction.cs:line 135 
    These exceptions are occurring on various EWS operations, but mostly on FindItems and Bind operations. They only seem to occur against Exchange 2010 servers at least that's what I've seen so far. These issues appear to resemble some kind of race condition.
    I did some searches on EWS, Race Condition, SyncTextWriter and I came across an interesting issue while searching on SyncTextWriter and Race Condition. I'm curious if this is what is causing this error to occur in the EWS library:
    http://connect.microsoft.com/VisualStudio/feedback/details/249253/synchronized-textwriter-not-synchronizing-properly
     The post indicates that SyncTextWriter was only designed to synchronize multiple threads against the same instance and that it was not designed to synchronize multiple threads against multiple instances SyncTextWriter. I've noticed that in all instances
    of this error I've seen the stack traces always end up inside of SyncTextWriter. Is this a bug in the EWS libraries use of SyncTextWriter? Note: the above article does post workarounds for this issue. Can't tell if the EWS library is using any of these workarounds
    or is avoiding this issue in some other way.
    I know Microsoft doesn't claim that the EWS library is thread safe. However, I've seen many posts that seem to indicate that if a separate instances of the exchange service are used for each thread it works fine. Futhermore, we've run this app many times against
    Office 365 and haven't seen this issue. It appears to only happen against Exchange 2010, but that maybe due to better throughput to the Exchange 2010 server compared to the Office 365 servers. Any help or insight would be greatly appreciated.
                 Thanks,
                      Steve
     

    Hi,
    Found a note explaining the significance of these errors.
    It says:
    "NZE-28862: SSL connection failed
    Cause: This error occurred because the peer closed the connection.
    Action: Enable Oracle Net tracing on both sides and examine the trace output. Contact Oracle Customer support with the trace output."
    For further details you may refer the Note: 244527.1 - Explanation of "SSL call to NZ function nzos_Handshake failed" error codes
    Thanks & Regards,
    Sindhiya V.

  • I am getting "Error in Loading Dll" when i instantiate AcroExch.App.

    I am getting "Error in Loading Dll" when i instantiate AcroExch.App.  This has worked find when i had Adobe Acrobat 9.0 and Adobe Reader 10.0 installed in my System.  Now the Adobe Reader 10.0 has been uninstalled and Adobe Reader 11.0.10 has been installed in my system.  Ever since this code is not working.  I have delveloped some critical applications in Exceh VBA and not it is not working.  Can anyone provide me the correct solution to overcome this issue.  Is this because the Acrobat and Reader version incompatibility.  If so, what is the workaround for this issue.

    Uninstall Adobe Reader and repair the installation of Adobe Acrobat.

  • Have MF8580Cdw. Brand new. Cannot get it to take paper from manual feed tray

    I have a new MF8580Cdw.  Still learning how to operate.  So far, I cannot get it to take paper (labels, etc) from the manual feed tray.  Solutions?

    Hi EHC.
    I will be glad to assist you.
    Please follow the steps below to turn on the tray and change the paper settings for labels:
    1.  Press (Select Paper/Settings).
    2.  Scroll to [Paper Settings], and then press [OK].
    3.  Scroll to [Multi-purpose Tray], and then press [OK].
    4.  Scroll to [ON], and then press [OK].
    5.  Scroll to [Paper Settings], and then press [OK].
    6.  Scroll to [Multi-purpose Tray], and then press [OK].
    7.  Scroll to [Set Details], and then press [OK].
    8.  Scroll to [LTR], and then press [OK].
    9.  Scroll to [Labels], and then press [OK].
    I hope this information is helpful to you.  Should you need further assistance, please contact us at 1-800-OK-CANON (1-800-652-2666).
    Did this answer your question? Please click the Accept as Solution button so that others may find the answer as well.

  • Cannot Get Sound Added to PDF from InDesign Export

    I am trying to export and InDesign document for the purpose of creating an interactive PDF.
    Here are the steps I am taking:
    1.     create indesign cs6 file;
    2.     export it as a swf file;
    3.     import swf file in new indesign cs6 document;
    4.     export the imported swf file as an interactive pdf file;
    5.     animations etc . . . all work great in pdf...BUT
    6.     no audio and i can't even add it in adobe acrobat pro x. (yes, i added file to resources tab in acrobat).
    thanks all.
    stephen

    HOW TO EXPORT EMBEDDED AUDIO or VIDEO AS AN INTERACTIVE PDF FILE (USING .SWF - INDESIGN and ACROBAT PRO)
    by Darren Mok
    I was teaching an InDesign class today and had the same problem where students could not embed their mp3 audio files into their exported PDF file (animation would work but no audio). I solved it last week for two students but forgotten how I did it - fortunately however, after hours of tinkering I finally found the answer again!
    This is what you have to do:
    1.     create indesign cs6 file; (including audio)
    2.     export it as a swf file; (check audio works - it should as swf in Flashplayer or html in browser) note RESOURCES folder containing audio files is also exported
    3.     import swf file in new indesign cs6 document;
    4.     export the imported swf file as an interactive pdf file; (animation will work but audio/video will NOT WORK!)
    5.    open PDF using Acrobat Pro and right-click to set the swf properties
    6.    add the exact Resources FOLDER directory that was exported when you exported the .swf (it should contain all the audio .mp3 files) - you cannot just simply add the .mp3s from ANY directory!!
    7.     walah it should now work! save the .pdf file and you should notice a file size increase
    8.     you can actually now delete the folder containing the audio (they are now embedded into the .pdf!) and the .pdf WILL PLAY AUDIO as one file!! WALAH!
    9.     repeat process to embed video files as well
    10.     your welcome
    *PLEASE NOTE: Checking the Properties/Resources in Acrobat Pro and adding a different audio directory or indivual files will NOT WORK if it isn't the same name as the exported folder when you exported the .SWF - IT MUST BE THE SAME DIRECTORY FOLDER NAME!!
    Let me know how you go and if you found this answer helpful. I agree it's an awkward way of doing it but I guarantee you it works!!
    Do I get an Adobe Tier 2 Level support badge now?
    Cheers.
    Darren Mok

  • Cannot get data of the row from OLE DB provider "OraOLEDB.Oracle" for linked server

    I have created a stored procedure in SQL Server for a report that uses parameters.  In the report I am linking an Oracle table.  I use a subquery like this to query the Oracle table:  (select * from openquery(oracle_linked_server, 'select
    partno, description from oracletable')).  If I run the subquery it works fine every time.  The linked server uses an oracle account which has access to the oracle table.  When I first created the Stored Procedure it worked fine for me.  When
    I test the report, it worked fine.  Then I asked another user to test it and it broke with the below error message.  
    OLE DB provider "OraOLEDB.Oracle" for linked server "XXXX_ORACLE" returned message "ORA-01403: no data found".
    Msg 7346, Level 16, State 2, Procedure usp_report_XXXXXX, Line 15
    Cannot get the data of the row from the OLE DB provider "OraOLEDB.Oracle" for linked server "XXXX_ORACLE".
    Now when I try the report or the stored procedure, I get the same error.  I tested the oracle subquery in the stored procedure and it still works.  The report uses a service account to execute the stored procedure.
    I am using SQL Server 2012 Developer Edition 64 bit (11.0.5058) Management Studio to develop the stored procedure.  The SQL Server I am accessing and running the stored procedure is SQL Server 2008R2 Developer Edition 64bit (10.50.2550).  The user
    that tested the report for me has SQL Server 2008R2 but that shouldn't matter since he is running the report in Internet Explorer.
    What is changing that it works for a while and then stops?
    Fred
    Fred Schmid

    I found the answer.  It was in the query.  I put the TRIM statement on the part# field in the Oracle subquery and took the LTRIM function out of the ON clause that joined my SQL Server table with the Oracle linked server table.  Now everything
    works.  The query looks like this:
    SQL_Server_Table sst
    LEFT OUTER JOIN
    (SELECT * FROM OPENQUERY(OracleLinkedServer, 'SELECT TRIM(partNo) AS partNo, partDesc FROM OracleTable')) ols
    ON sst.partNo = ols.partNo
    Thanks for pointing me in the right direction.
    Fred Schmid

  • I have an O S X 10.8.5 and an epsom sx600fw and i cannot get it to print anything from textedit or any other text although it will make copies . please help

    i have an O S X 10.8.5 and an epsom SX600 FW printer and i cannot get it to print from Textedit or anything else ! although it seems to do coloured borders but not text. it does make copies ,but will not print. please help ,i urgently need to type and print a letter.

    i have an O S X 10.8.5 and an epsom SX600 FW printer and i cannot get it to print from Textedit or anything else ! although it seems to do coloured borders but not text. it does make copies ,but will not print. please help ,i urgently need to type and print a letter.

  • I have an acer aspire TC-220 WIN 10 I cannot get my pc to boot from CD drive this is driving me mad

    HiI have an Acer Aspire TC-220 desktop 16gb ram 3 tb hard drive AMD A-10-7800 RUNNING WINDOWS 10This is driving me mad I cannot for the life of me get my PC to boot from the CD drive I believe you have to change the drive in Bios from the UEFI drive to a legacy drive there is no provision to do this I have tried everything in the Bios nothing works I have set the 1st priority as the cd drive please can you helpmany thanks

    ecneps1948,
    Is the optical drive being seen and is usable in Windows itself?

  • When I installed the latest download, I find I cannot get my playlist to download from my printer - it justs " Scunches the wording into a 2inch gap at top of printout - any ideas

    After installing one of latest downloads - I found I cannot get a print out of the C.D. I have downloaded - all I get from my Printer is a list squashed into the top 1 to 2 inches at top of page - making it inpossible to read - any Ideas how to solve ???????????? My Printer is a HP PHOTOSMART 3210

    I suspect we need to await a fix in the next build...
    tt2

Maybe you are looking for

  • Error message needs to be generated in case of change in released PO.

    Hi all, please help me how to solve this issue <b>issue1:</b> Error message needs to be generated in case of change in released PO. 1) change in incoterms,   2) payment terms,                  3) quantity                              4) rate.        

  • When posting, please post a new topic if you don't have an answer!

    Occasionally you may be tempted to add your voice to a chorus of complaints. Unfortunately, if what you are here for is to get assistance for a problem which you can't find an answer for, trailing an existing thread with a second complaint, is usuall

  • Duplicate the explorer window (Ctrl+N)

    Hi I'm using JSP and servlet that running in tomcat4.0 the problem is that if i'm choosing from the menu File-> New->Window (or pressing the Ctrl+N) it duplicate the jsp with the same session id and it cause problems. e.g the user can press the "Upda

  • IPhoto Library file "grayed out"

    When I start up iPhoto, it prompts me to choose a library. My library file is "grayed out", not letting me choose it. Any help? I'd like to try and keep all my albums if I can. Thanks!

  • Newbie Setup - Fail2ban & MSMTP

    Hi Guys I've installed arch a couple of days ago and so far so good until today... I installed msmtp instead of ssmtp due to the lack of development now, that's configured and can send mail through my gmail account.  I then installed fail2ban and log